我收到错误
对象引用未设置为对象的实例
public static void SaveDashboardStream(string connectionString, MemoryStream ms)
{
using (DashboardDBConDataContext db = new DashboardDBConDataContext(connectionString))
{
DBDashboard storedDashboard = (from t in db.Dashboards
select t).FirstOrDefault();
ms.Seek(0, SeekOrigin.Begin);
storedDashboard.DashboardStream = new Binary(ms.ToArray());
db.SubmitChanges();
}
}
我在函数As中使用上面的代码
private void DashBoardGenerator_DashboardSaving(object sender, DashboardSavingEventArgs e)
{
// Determines whether the user has called the Save command.
if (e.Command == DashboardSaveCommand.Save)
{
try
{
using (MemoryStream saveStream = new MemoryStream())
{
Dashboard.SaveToXml(saveStream);
DashboardSql.SaveDashboardStream(dashboardStorageConnectionString, saveStream);
}
e.Handled = true;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Unable to Save Dashboard", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
如果我错在任何地方,请提供指导?
答案 0 :(得分:0)
可能storedDashboard
似乎是空的。
using (DashboardDBConDataContext db = new DashboardDBConDataContext(connectionString))
{
DBDashboard storedDashboard = (from t in db.Dashboards
select t).FirstOrDefault();
//Always check for null when selecting First() or FirstOrDefault()
if(storedDashboard == null) {
storedDashboard = new DBDashboard();
}
ms.Seek(0, SeekOrigin.Begin);
storedDashboard.DashboardStream = new Binary(ms.ToArray());
db.SubmitChanges();
}