Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to fetch list of values from DB and display them in a dropdown, how can i fetch without passing anything.

what to use in place of sqlparams??
C#
public List<string>  GetTimeZones()
{
    using (SqlDataReader dr = SqlHelper.ExecuteReader(DatabaseConnection.Connection,CommandType.Text, "sp_EP_GetTimeZones"))
    {
        if (dr.Read())
        { 
           
        
        }   
    }
}


SQL
ALTER procedure [dbo].[sp_EP_GetTimeZones] 
AS
BEGIN
  Select Zonetime from dbo.EP_TimeZones
  END
Posted
Updated 5-May-15 23:41pm
v6
Comments
Tomas Takac 6-May-15 5:38am    
What do you mean? I don't see any parameters.
chandra sekhar 6-May-15 5:39am    
I just have to fetch values from the DB to show in a drodown
OriginalGriff 6-May-15 5:38am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

I guess you are looking for this,hope it will help you

public DataTable GetAllEmployees()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(conString);

try
{
SqlCommand cmd = new SqlCommand("StoredprocedureName", con);
cmd.CommandType = CommandType.StoredProcedure;

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}

At last Convert DataTable to List as per your requirements.
 
Share this answer
 
v2
Comments
Do you realize that the return type is DataTable, but you are not returning anything?
You can return a DataTable and bind that directly. Let me update your code.
C#
public DataTable GetTimeZones()
{
    DataTable dtTimeZones = new DataTable();

    SqlCommand cmd = new SqlCommand("sp_EP_GetTimeZones;", con);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(dtTimeZones);
    
    return dtTimeZones;
}

While binding, you can do like...
C#
ddlTimeZones.DataSource = GetTimeZones();
ddlTimeZones.DataTextField = "Zonetime";
ddlTimeZones.DataValueField = "Zonetime";
ddlTimeZones.DataBind();

I have assigned Zonetime for to both Value and ID. If you have any other column value, then just fetch that in procedure and update here.
 
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