[英]“Failed to convert parameter value from a DataRowView to a Int32” message on running a code
我正在嘗試從.Net加載一些存儲在SQL Server上的圖像數據,但是在屏幕上出現以下錯誤消息時出現錯誤:無法將參數值從DataRowView轉換為Int32,但我不知道問題出在哪里。
這部分的代碼:
try
{
conn.Open();
strSQL = "SELECT * FROM Pictures WHERE ID = @ID";
da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.Parameters.Add("@ID" ,SqlDbType.Int).Value = listBox1.SelectedValue;
ds = new DataSet();
da.Fill(ds, "Pictures");
byte[] arrPic = (byte[])(ds.Tables["Pictures"].Rows[0]["Pic"]);
MemoryStream ms = new MemoryStream(arrPic);
pictureBox1.Image = Image.FromStream(ms);
conn.Close();
}
catch (SystemException ex)
{
MessageBox.Show(ex.Message);
}
錯誤出現在“ da.Fill(ds,“ Pictures”);“上
listBox1.SelectedValue
返回一個對象,而不是整數。 嘗試像這樣轉換
da.SelectCommand.Parameters.Add("@ID" ,SqlDbType.Int).Value = Convert.ToInt32(listBox1.SelectedValue);
正如@Kevin在其評論中指出的那樣,如果listBox
實際上返回了整個DataRowView
,則可能必須使用其他方法。 就像是
DataRowView drv = (DataRowView)listBox1.SelectedItem;
String valueOfItem = drv["TheColumnYouActuallyWant"].ToString();
da.SelectCommand.Parameters.Add("@ID" ,SqlDbType.Int).Value = Convert.ToInt32(valueOfItem);
如該問題所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.