当我选择datagrid1时,它会显示错误
object reference not set
请帮我解决。我是wpf的新手。
private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (this.dataGrid1.SelectedItems == null || this.dataGrid1.SelectedItems.Count <= 0) return;
this.db = new DbAccess();
string str = this.dataGrid1.SelectedCells[0].Item.ToString();
DataTable dataTable =
this.db.DataTab(
@"SELECT [SeriesNo], [SeriesDescription], [Instances]
FROM [Roamani].[dbo].[View_Study123]
WHERE[StudyUID] = '" + str +"'");
string[] strArray = new string[4];
if (dataTable.Rows.Count > 0)
{
strArray[0] = str;
strArray[1] = dataTable.Rows[0]["SeriesNo"].ToString();
strArray[2] = dataTable.Rows[0]["SeriesDescription"].ToString();
strArray[3] = dataTable.Rows[0]["Instances"].ToString();
}
}
答案 0 :(得分:0)
用下面提到的方法替换你的方法并更改conncetion字符串。
public DataTable DataTab(string query)
{
string conncetion = "Data Source=(local);Initial Catalog=Temp;User ID=sa;Password=sa123*";
SqlConnection cnn = new SqlConnection(conncetion);
string str = string.Empty;
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = cnn;
if (cnn.State == ConnectionState.Closed)
cnn.Open();
DataTable dataTable = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(query, cnn);
adp.Fill(dataTable);
return dataTable;
}
仍然是null,然后尝试使用此查询
@"SELECT [SeriesNo], [SeriesDescription], [Instances]
FROM [View_Study123].[dbo].[Roamani]
WHERE[StudyUID] = '" + str +"'");
此处View_study123是您的数据库名称,Romani是您的表名