2016-08-25 55 views
0

我有這個存儲過程與提供的參數得到產品表嘗試通過選擇組合框中的項目來自動填充文本框。表單打開時出錯。

CREATE PROCEDURE DisplayProductParameter @id nvarchar(100) 
AS 
BEGIN 
SET NOCOUNT ON; 
    SELECT P.product_id, P.product_name, P.product_price, T.[type_name], T.[type_fee], T.[type_id] 
    FROM Product P 
    INNER JOIN [Product Type] T ON P.[type_id] = T.[type_id] 
    WHERE P.product_id = @id 
END; 

GO

我用這個功能在C#中調用它

public SqlCommand InitSqlCommand(string query, CommandType commandType) 
    { 
     var Sqlcommand = new SqlCommand(query, con); 
     Sqlcommand.CommandType = commandType; 
     return Sqlcommand; 
    } 

然後我將其存儲在一個DataTable

public DataTable GetData(SqlCommand command) 
    { 
     var dataTable = new DataTable(); 
     var dataSet = new DataSet(); 
     var dataAdapter = new SqlDataAdapter { SelectCommand = command }; 
     dataAdapter.Fill(dataTable); 
     return dataTable; 
    } 

那麼這就是我如何獲得DataT能夠

public DataTable DisplayProductParameter() 
    { 
     string getProductIdParam = "DisplayProductParameter"; 
     var command = Connection.InitSqlCommand(getProductIdParam, CommandType.StoredProcedure); 
     command.Parameters.AddWithValue("@id", P.Id); 
     return Connection.GetData(command); 
    } 

這是我應該怎麼自動填充文本框,每當我點擊下拉框

private void cmbProductId_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      p.Id = cmbProductId.SelectedItem.ToString(); 
      dtbProduct = po.DisplayProductParameter(); 
      for (int i = 0; i < dtbProduct.Rows.Count; i++) 
      { 
       txtProductType.Text = dtbProduct.Rows[i]["type_name"].ToString(); 
       txtPrice.Text = dtbProduct.Rows[i]["product_price"].ToString(); 
       txtProductName.Text = dtbProduct.Rows[i]["product_name"].ToString(); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

但我在表格

過程或函數'開始收到此錯誤信息DisplayProductParameter'預計參數 '@id',它沒有提供。

回答

0

邏輯上你的代碼看起來是正確的。 爲了得到在哪裏,以及爲什麼發生這種情況的更多信息,你可以在此行中添加斷點:

public DataTable DisplayProductParameter() 
    { 
     string getProductIdParam = "DisplayProductParameter"; 
     var command = Connection.InitSqlCommand(getProductIdParam, CommandType.StoredProcedure); 
    -->command.Parameters.AddWithValue("@id", P.Id); 
     return Connection.GetData(command); 
    } 

調試模式運行看什麼的P.Id值。它可能會傳遞一個空或空的字符串值到過程中。

相關問題