2016-08-25 47 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值。它可能会传递一个空或空的字符串值到过程中。

相关问题