Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i select Dropdownlist items i have to show particular item data into listbox
how can i do this


this is my storeprocedure...

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

AS
BEGIN
     SELECT full_name FROM Players WHERE player_id = @player_id



END

this is my cs code...


C#
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;
           }
Posted
Updated 22-Jun-12 0:25am
v2
Comments
[no name] 22-Jun-12 7:36am    
What is "qry" doing here? Can you please tell me?
Vani Kulkarni 25-Jun-12 0:03am    
Did you get the solution?

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:
C#
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.
C#
ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "player_id";
 
Share this answer
 
Comments
Manas Bhardwaj 22-Jun-12 7:21am    
+5
You are missing this line of code:
C#
DataSet ds = new DataSet();

You need to write it before you fill it, i.e., before the below line:
C#
da.Fill(ds);
 
Share this answer
 
Comments
Sandeep Mewara 22-Jun-12 6:38am    
I doubt if this is the reason.

'ds' must have been initialized earlier.
rockpune 22-Jun-12 6:39am    
still not getting dropdownlist value into listbox

Hi,

Try this:
C#
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;
           }
}


All the best.
--AK
 
Share this answer
 
v5
Comments
[no name] 22-Jun-12 7:45am    
Can anyone tell me that what is the problem with my solution? simply down-voted?
Sandeep Mewara 22-Jun-12 8:19am    
Let me see what could be the possible reason.

Your answer is same as what solution 1 was told + con.Open() suggestion.
I would like to share with you that while using SQLDataAdapter you don't need to open the connection. It's the adapter that by itself takes care of opening and closing of connection.


Did you see my answer?
rockpune 22-Jun-12 8:02am    
Error converting data type nvarchar to int.
Sandeep Mewara 22-Jun-12 8:17am    
Dude, did you try or even followed my answer? :doh:
[no name] 22-Jun-12 8:16am    
Now check my updated answer..
C#
con = new SqlConnection(connection);
dt= new datatable();
SqlCommand command = new SqlCommand("[dbo].[StoredProc1]", con); 
command = new SqlCommand(qry, con);
SqlParameter("@player_id",DropDownList1.SelectedValue));
command.Parameters.Add("@player_id", SqlDbType.int).Value = DropDownList1.SelectedValue;
command.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = command;
da.Fill(dt);
con.close();

ListBox1.DataSource = dt[0];
ListBox1.DataTextField = "playerID";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900