2013-05-27 111 views
1

此錯誤將出現在我的代碼過程或函數'gridalldata'期望參數'@order_no',這是沒有提供。我喜歡下面如何通過Sql參數

try 
{ 
    con.Open(); 

    SqlCommand cmd = new SqlCommand("gridalldata", con); 
    cmd.Parameters.Add("@order_no", SqlDbType.NVarChar).Value = txt_orderno.Text; 

    SqlDataReader dr = cmd.ExecuteReader(); 

    for (int i = 0; i < dataGridView1.Rows.Count; i++) 
    { 
     if (dr.HasRows) 
     { 
      dr.Read(); 

      dataGridView1.Rows[i].Cells[0].Value = dr[0].ToString(); 

      dataGridView1.Rows[i].Cells[2].Value = dr[2].ToString(); 
      dataGridView1.Rows[i].Cells[3].Value = dr[3].ToString(); 
      dataGridView1.Rows[i].Cells[4].Value = dr[4].ToString(); 
      dataGridView1.Rows[i].Cells[5].Value = dr[5].ToString(); 
      dataGridView1.Rows[i].Cells[6].Value = dr[6].ToString(); 
      dataGridView1.Rows[i].Cells[7].Value = dr[7].ToString(); 
      dataGridView1.Rows[i].Cells[8].Value = dr[8].ToString(); 
      dataGridView1.Rows[i].Cells[9].Value = dr[9].ToString(); 
      dataGridView1.Rows[i].Cells[10].Value = dr[13].ToString(); 
      dataGridView1.Rows[i].Cells[11].Value = dr[12].ToString(); 
     } 

    } 
    dr.Close(); 
    con.Close(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 
finally 
{ 
    con.Close(); 
} 

如何解決這一問題

+1

您需要設置命令類型 –

+0

實際上是cmd.CommandType = CommandType.StoredProcedure – Serge

+0

我在設置,但同樣的錯誤來了 – naeemshah1

回答

4

使用cmd.CommandType = CommandType.StoredProcedure;執行存儲過程的參數發送到程序。

+0

但如何設置GridView的數據即將 – naeemshah1

+0

好數據,將是有趣的運行「補」,並把結果放到一個DataTable,像這樣: DbDataAdapter的適配器=新的SqlDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dataTable); – PiLHA

+0

@PiLHA:好的做法是使用dataadaptor填充dataGrid或Gridview,我回答了一些像 –

3

試試這個:

Try 
     { 
     con.Open(); 
      string order= txt_orderno.Text; 

      SqlCommand cmd = new SqlCommand("gridalldata", con); 
       cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.AddWithValue("@order_no", SqlDbType.NVarChar).Value=order; 

      SqlDataReader dr = cmd.ExecuteReader(); 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
      if (dr.HasRows) 
      { 
       dr.Read(); 

       dataGridView1.Rows[i].Cells[0].Value = dr[0].ToString(); 

       dataGridView1.Rows[i].Cells[2].Value = dr[2].ToString(); 
       dataGridView1.Rows[i].Cells[3].Value = dr[3].ToString(); 
       dataGridView1.Rows[i].Cells[4].Value = dr[4].ToString(); 
       dataGridView1.Rows[i].Cells[5].Value = dr[5].ToString(); 
       dataGridView1.Rows[i].Cells[6].Value = dr[6].ToString(); 
       dataGridView1.Rows[i].Cells[7].Value = dr[7].ToString(); 
       dataGridView1.Rows[i].Cells[8].Value = dr[8].ToString(); 
       dataGridView1.Rows[i].Cells[9].Value = dr[9].ToString(); 
       dataGridView1.Rows[i].Cells[10].Value = dr[13].ToString(); 
       dataGridView1.Rows[i].Cells[11].Value = dr[12].ToString(); 
       } 

       } 
      dr.Close(); 
      con.Close(); 


     } 
0
using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    connection.Open(); 
    String orderNo=txt_orderno.Text; 
    // Am assuming gridalldata is your SP 
    SqlCommand cmd= new SqlCommand(gridalldata, connection); 
    cmd.CommandType=CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@order_no", orderNo); 
    SqlDataReader reader = command.ExecuteReader(); 
    while (reader.Read()) 
    { 
     // Code   
    } 
} 

using聲明確保連接在使用後關閉

你也應該結合你的DataGridView這樣

Public DataTable FillDataGrid(string orderID) 
{ 

SqlCommand cmd = new SqlCommand("gridalldata", conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@order_no", orderNo); 
     SqlDataAdapter dap = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     dap.Fill(ds); 
     return ds.Tables[0]; 

} 

Datatable dt=FillDataGrid(txt_orderno.Text); 
DataGridVIew1.DataSource=dt; 
+0

它不工作,因爲我有gridview列和@order_id有10行和15列10列gridview和5文本框 – naeemshah1

+0

如果它的'GridView'然後發佈您的Gridveiw標記它的幫助讀者很容易回答, –