简体   繁体   English

C#错误:“填充:SelectCommand.Connection属性尚未初始化。”

[英]C# Error: “Fill: SelectCommand.Connection property has not been initialized.”

I'm using this video to try and populate results from a DataGridView , and am receiving the above error. 我正在使用此视频尝试从DataGridView填充结果,并收到上述错误。 The below code pertains to this error - I pass values into a stored procedure, then the final SELECT returns the values in the table in the DataGridView . 下面的代码与此错误有关 - 我将值传递给存储过程,然后最终的SELECT返回DataGridView中表中的值。

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "integrated security=SSPI;data source=SERV;" + "persist security info=False;initial catalog=DB";

        con.Open();
        SqlCommand select = new SqlCommand("SELECT * FROM Table");
        SqlCommand enter = new SqlCommand("sp_Proc", con);

        // Stored Procedure
        enter.CommandType = CommandType.StoredProcedure;
        enter.Parameters.Add(new SqlParameter("@vvalue", SqlDbType.VarChar)).Value = Convert.ToString(txt1.Text);
        enter.Parameters.Add(new SqlParameter("@dvalue", SqlDbType.Decimal)).Value = Convert.ToDecimal(txt2.Text);
        enter.ExecuteNonQuery();

        // DataGrid returns the SELECT

        SqlDataAdapter sadapt = new SqlDataAdapter(select);
        sadapt.SelectCommand = select;
        DataTable dtab = new DataTable();
        sadapt.Fill(dtab); // generates the error
        BindingSource b = new BindingSource();
        b.DataSource = dtab;
        dGrid.DataSource = b;
        sadapt.Update(dtab);

        con.Close();

You did not pass connection object on the command . 您没有在command上传递connection对象。 Try this instead, 试试这个,

SqlCommand select = new SqlCommand("SELECT * FROM Table", con);

You are passing connection object to your enter command but didnt pass the connection object to your select command 您正在将连接对象传递给您的enter命令,但没有将连接对象传递给您的select命令

  SqlCommand select = new SqlCommand("SELECT * FROM Table");
  SqlCommand enter = new SqlCommand("sp_Proc", con);

Use this 用这个

SqlCommand select = new SqlCommand("SELECT * FROM Table",con);
SqlCommand cmd = new SqlCommand("sp_StockAnalysis2") // connection missing in sqlcommand

SqlCommand cmd = new SqlCommand("sp_StockAnalysis2",Connection())// pass connection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误:“填充:SelectCommand.Connection属性尚未初始化。” - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” C#错误:“填充:SelectCommand.Connection属性尚未初始化。”仅在PC上 - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” just in on pc 填充:SelectCommand.Connection 属性尚未初始化 C# - Fill: SelectCommand.Connection property has not been initialized C# 填充:SelectCommand.Connection属性尚未初始化错误 - Fill: SelectCommand.Connection property has not been initialized error 错误填充:SelectCommand.Connection属性尚未初始化 - error Fill: SelectCommand.Connection property has not been initialized SelectCommand.Connection属性尚未初始化。 MySQL的 - The SelectCommand.Connection property has not been initialized. MySql 填充:SelectCommand.Connection属性尚未初始化。 该怎么办? - Fill: SelectCommand.Connection property has not been initialized. what to do? 如何修复“填充:SelectCommand.Connection 属性尚未初始化”。 - How to fix 'Fill: SelectCommand.Connection property has not been initialized.' 未处理无效的操作异常(其他信息:填充:SelectCommand.Connection属性尚未初始化。) - invalid operation exception was unhandled (Additional information: Fill: SelectCommand.Connection property has not been initialized.) 填充:SelectCommand.Connection属性尚未初始化 - Fill: SelectCommand.Connection property has not been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM