2013-07-04 142 views
0

我想從數據庫中檢索數據。當我改變我的代碼來使其顯示參數無效時。參數無效

private void button7_Click(object sender, EventArgs e) 
{   
    ProductDetails.Items.Clear(); 
    SqlConnection con = new SqlConnection(@"server=xxx-PC; database= sample; integrated security= true"); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("select * from tblproduct where prodname like '" + textBox1.Text + "%';", con); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    while (dr.Read()) 
    { 
     byte[]imgg =(byte[])(dr["image"]); 
     if(imgg==null) 
      pictureBox1.Image= null; 
     else 
     {     //i m not getting error it says parameter not valid below// 
      MemoryStream mstream = new MemoryStream(imgg); 
      pictureBox1.Image = System.Drawing.Image.FromStream(mstream); 
     } 

     ProductDetails.Items.Add(dr[0].ToString() + " \t" + dr[1].ToString() + "\t" + dr[2].ToString()+ dr[3].ToString());    
    } 
} 

加入得自OP的評論

加載我用這個代碼

byte[] imagebt = null; 
FileStream fstream = new FileStream(this.textBox5.Text, FileMode.Open, FileAccess.Read); 
BinaryReader br = new BinaryReader(fstream); 
imagebt = br.ReadBytes((int)fstream.Length); 
+5

沒有錯誤發生在哪一行? – Tim

+5

你應該看看[sqlParameter](http://msdn.microsoft.com/en-ca/library/system.data.sqlclient.sqlparameter%28v=vs.110%29.aspx)。你的代碼對於[sql注入](http://en.wikipedia.org/wiki/SQL_injection) –

+2

可能是重複http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system -drawing-image –

回答

0

請讓我知道,如果這個工程:

private void button7_Click(object sender, EventArgs e) 
{   
    ProductDetails.Items.Clear(); 
    SqlConnection con = new SqlConnection(@"server=xxx-PC; database= sample; integrated security= true"); 

    SqlCommand cmd = new SqlCommand("select * from tblproduct where prodname like @name;", con); 
    cmd.Parameters.AddWithValue(textBox1.Text.Trim() + "%"); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable  dt = new DataTable(); 
    try 
    { 
     con.Open(); 
     da.Fill(dt); 
    } 
    catch (Exception e) 
    { //exception handling here } 
    finally { con.Close(); } 

    foreach(DataRow dr in dt.Rows) 
    { 
     byte[]imgg =(byte[])dr["image"]; 
     if(imgg==null || imgg.length <= 0) 
      pictureBox1.Image= null; 
     else 
     { 
      pictureBox1.Image = ByteToImage(imgg); 
     } 

     ProductDetails.Items.Add(dr[0].ToString() + " \t" + 
      dr[1].ToString() + "\t" + 
      dr[2].ToString() + 
      dr[3].ToString());    
    } 
} 

// https://stackoverflow.com/questions/9576868/how-to-put-image-in-a-picture-box-from-a-byte-in-c-sharp 
public static Bitmap ByteToImage(byte[] blob) 
{ 
    MemoryStream mStream = new MemoryStream(); 
    byte[] pData = blob; 
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length)); 
    Bitmap bm = new Bitmap(mStream, false); 
    mStream.Dispose(); 
    return bm; 
} 

您也可以使用usingSqlConnection塊如在this SO question中那樣。 另請注意,最好是而不是使用Select * from ...但要命名列。 你可以閱讀更多關於它的這些鏈接:

+0

不,它沒有給我輸出,也沒有錯誤 – pooja

+1

@pooja你沒有足夠的具體程度來讓人們來幫助你。你現在被問了兩次,「錯誤發生在哪條線上?」,但你仍然沒有給出答案。因此,請編輯您的問題,以包含導致錯誤的確切行,以及**您正在獲取的確切錯誤消息。 –

+0

錯誤:連接未關閉 – pooja