我的代码有什么错误? [英] Whats the error in my Code?
本文介绍了我的代码有什么错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的荣誉,
在我的Windows应用程序中,存储并检索图像...存储图像时没有问题..但是,当我从数据库中获取图像数据时,以下错误正在上升...
Dear Honors,
In my windows application am store and retrieve the images... There is no problem while store the image.. But When I fetch the image data from database the following error is rising...
System.ArgumentException: Parameter is not valid.
这是我的代码...
如果可能,请更改我的代码....
Here is my code...
If possible kindly alter my code....
private void cmbID_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Query = "Select Photo from tblTest where ID = '" + cmbID.Text + "'";
SqlCmd = new SqlCommand(Query, SqlCon);
SqlDR = SqlCmd.ExecuteReader();
if (SqlDR.Read())
{
byte[] imageData = (byte[])SqlDR["Photo"];
Image newImage;
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}
//set picture
pictureBox1.Image = newImage;
}
SqlDR.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
推荐答案
使用调试器,尝试缩小获取异常的行范围.
一个可能的原因是因为yuo在您的SQL语句中使用了文字.您应该使用 SqlParameter [
Using debugger, try to narrow down from what line you''re getting the exception.
One possible cause is because yuo have used literals in your SQL statement. You should use SqlParameter[^] in any case so your query would look like:
SqlCmd = new SqlCommand(Query, SqlCon);
Query = "Select Photo from tblTest where ID = @id";
SqlCmd.Parameters.AddWithValue("@id,"cmbID.Text);
SqlDR = SqlCmd.ExecuteReader();
这篇关于我的代码有什么错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文