SelectCommand属性在调用"Fill"之前尚未初始化……任何人都知道这是什么错误 [英] The SelectCommand property has not been initialized before calling 'Fill'......any one tell what error this

查看:90
本文介绍了SelectCommand属性在调用"Fill"之前尚未初始化……任何人都知道这是什么错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择下拉列表项时,我必须将特定项数据显示到列表框中
我该怎么办


这是我的存储过程...

when i select Dropdownlist items i have to show particular item data into listbox
how can i do this


this is my storeprocedure...

CREATE PROCEDURE [dbo].[StoredProc1]
@player_id int

AS
BEGIN
     SELECT full_name FROM Players WHERE player_id = @player_id



END


这是我的CS代码...



this is my cs code...


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
       {
           try
           {

                   if (DropDownList1.SelectedItem != null)
                   {
                      
                       con = new SqlConnection(connection);
                       SqlCommand command = new SqlCommand("[dbo].[StoredProc1]", con);

                       command = new SqlCommand(qry, con);
                      SqlParameter("@player_id",                       DropDownList1.SelectedValue));

                       command.Parameters.Add("@player_id", SqlDbType.NVarChar).Value = DropDownList1.SelectedValue;
                       command.CommandType = CommandType.StoredProcedure;
                       SqlDataAdapter da = new SqlDataAdapter(cmd);

                       da.Fill(ds);
                        ListBox1.DataSource = ds;
                       ListBox1.DataTextField = "player_id";
                       ListBox1.DataBind();

                   }
           }
           catch (Exception ex)
           {
               throw ex;
           }

推荐答案

查看如何将SQLDataAdapter与命令作为参数一起使用:MSDN:SqlDataAdapter构造函数(SqlCommand) [
See on how SQLDataAdapter is used with command as parameter: MSDN: SqlDataAdapter Constructor (SqlCommand)[^]

Internally, it''s the Select command that is initialized for it.

Try to use it like this as it will be simple to read and understand:
con = new SqlConnection(connection);
SqlCommand command = new SqlCommand("[dbo].[StoredProc1]", con); 
command = new SqlCommand(qry, con);
SqlParameter("@player_id",DropDownList1.SelectedValue));
command.Parameters.Add("@player_id", SqlDbType.NVarChar).Value = DropDownList1.SelectedValue;
command.CommandType = CommandType.StoredProcedure;
// changed this line
SqlDataAdapter da = new SqlDataAdapter();
// changed this line
da.SelectCommand = command;
da.Fill(ds);




此外,您需要将列表与数据表而不是数据集绑定.




Further, you need to bind the list with a datatable and not dataset.

ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "player_id";


您缺少以下代码行:
You are missing this line of code:
DataSet ds = new DataSet();


您需要先填写它,然后再填写以下行:


You need to write it before you fill it, i.e., before the below line:

da.Fill(ds);




试试这个:
Hi,

Try this:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
           try
           {
 
                   if (DropDownList1.SelectedItem != null)
                   {
                      
                       con = new SqlConnection(connection);
                       con.Open();//This is required always
                       SqlCommand command = new SqlCommand("[dbo].[StoredProc1]", con);
 
                       //command = new SqlCommand(qry, con); *not required
                       command.CommandType = CommandType.StoredProcedure; //Replaced. before adding parameter must declare command type
 
                       command.Parameters.Add("@player_id", SqlDbType.NVarChar).Value = DropDownList1.SelectedValue;

                       SqlDataAdapter da = new SqlDataAdapter(command);

                       DataSet ds=new DataSet();

                       da.Fill(ds);
                       con.Close();
                       ListBox1.DataSource = ds;
                       ListBox1.DataTextField = "player_id";
                       ListBox1.DataBind();
 
                   }
           }
           catch (Exception ex)
           {
               throw ex;
           }
}



祝一切顺利.
--AK



All the best.
--AK


这篇关于SelectCommand属性在调用"Fill"之前尚未初始化……任何人都知道这是什么错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆