Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
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
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;
           }
Posted 22-Jun-12 0:19am
Edited 22-Jun-12 0:25am

Comments
Amit Kmr Sinha - 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?

4 solutions

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";
  Permalink  
Comments
Manas Bhardwaj - 22-Jun-12 7:21am
+5
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);
  Permalink  
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:
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
  Permalink  
Comments
Amit Kmr Sinha - 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:
Amit Kmr Sinha - 22-Jun-12 8:16am
Now check my updated answer..
rockpune - 22-Jun-12 8:36am
same error not getting any output
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";
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 544
1 Michael Haephrati 390
2 OriginalGriff 233
3 Ron Beyer 220
4 samadhan_kshirsagar 219
0 Sergey Alexandrovich Kryukov 6,959
1 Prasad_Kulkarni 3,689
2 OriginalGriff 3,402
3 _Amy 3,332
4 CPallini 2,950


Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 22 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid