Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone!

I am new to this, so I was hoping if someone could point me in the direction of a tutorial or a code sample. I have seen a bunch of stuff on the web, with this particular piece done in VB. I have my stored procedures in SQL & tables in place. I am trying to populate an asp dropdownlist from the records of a stored procedure using dataset. Does anyone know how to do this in C#?

I am using 3 layer architechure

I hope u guys can help me out.
Thanks

I am
Posted
Updated 27-Nov-12 20:41pm
v2
Comments
sachinkale20 28-Nov-12 8:46am    
u want only using dataset or even through reader will also suffice???

Hi,

Try with the below code.

aspx page code for dropdownlist
C#
<asp:dropdownlist id="ddlName" runat="server" datatextfield="<%# Eval("TextFieldColumnName") %>" datavaluefield="<%# Eval("ValueFieldColumnName")%>" xmlns:asp="#unknown">
       
        </asp:dropdownlist>


Code Behind
C#
public DataSet GetItems()
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("procedureName;", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    try
    {
        con.Open();
        da.Fill(ds);
        con.Close();       
    }
    catch (Exception ex)
    {
        //write error message
    }  
     return ds; 
}


bind Dropdown
C#
ddlName.DataSource = GetItems();
ddlName.DataBind();
 
Share this answer
 
Comments
msweety 28-Nov-12 5:19am    
Hi Mohd,

thanks for the response it helped me...
could you pls let knoe where to write the bind dropdown code?

thanks in advance :-)
Mohd. Mukhtar 28-Nov-12 5:27am    
In code behind file.

write the code in Page_Load method with as below.

if(!IsPostback) //This will make sure that only once the below code will be executed.
{
ddlName.DataSource = GetItems();
ddlName.DataBind();
}
msweety 28-Nov-12 5:36am    
thanks again could you also pls help in wirting the code under the dropdown list button.?

protected void ddlLobGrpId_SelectedIndexChanged(object sender, EventArgs e)
{


}
Mohd. Mukhtar 28-Nov-12 6:02am    
We can not write code here as we don't know functionality, and what do you want to perform here.
msweety 28-Nov-12 6:11am    
under .cs page I would like to dynamically fill the dataset with a stored procedure here
This is not the exact solution for your problem but I hope this will help you to get some idea to solve your problem.

This is the SP I used for this sample

SQL
CREATE proc [dbo].[ReadAllImageIDs] as 
SELECT ImageID FROM ImageData 
GO


below is the loadImageIDs method

C#
private void loadImageIDs()
{
    #region Load Image Ids
    SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
    SqlCommand cmd = new SqlCommand("ReadAllImageIDs", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            cmbImageID.DataSource = dt;
            cmbImageID.ValueMember = "ImageID";
            cmbImageID.DisplayMember = "ImageID";
            cmbImageID.SelectedIndex = 0;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
    #endregion
}


for more details
Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]

Thanks
 
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