Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a dropdown which has month name as text field and int like 1,2,3 as value .
Now in database the value is stored for month.

On scr2 when Edit button is clicked ,I am getting all the values and binding to the page fields but how to bind dropdown with value field of 1?

I have used below logic to bind dropdown:

C#
System.Globalization.DateTimeFormatInfo dateTimeInfo = new System.Globalization.DateTimeFormatInfo();
            for (int i = 1; i < 13; i++)
            {
                ddlMonths.Items.Add(dateTimeInfo.GetMonthName(i).ToString());



            }

What I have tried:

i tried using

ddlMonths.selectedvalue = session[]  // ( the value from column is in session )
Posted
Updated 22-May-16 19:16pm
Comments
Karthik_Mahalingam 23-May-16 0:59am    
few queries
1) what is the database column type for month ( storing as 1,2,3 or january, feb ... )
2) in both the drop down how you are populating the values ( 1,2,3 or jan feb .. )
AlwzLearning 23-May-16 1:05am    
Hi. It is smallInt in DB and we are storing it as 1,2,3..but while displaying have to show it as jan, feb , march.
Karthik_Mahalingam 23-May-16 1:05am    
Always use  Reply  button, to post Comments/query to the user, else the User wont get notified.
AlwzLearning 23-May-16 1:07am    
ok got it. Thankyou .
Karthik_Mahalingam 23-May-16 1:16am    
check the solution.

1 solution

For binding the dates use

C#
System.Globalization.DateTimeFormatInfo dateTimeInfo = new System.Globalization.DateTimeFormatInfo();
               for (int i = 1; i < 13; i++)
               {
                   string monthName = dateTimeInfo.GetMonthName(i);
                   ddlMonths.Items.Add(new ListItem(monthName, i.ToString()));
               }

To get Selected Value
C#
int monthNumber = Convert.ToInt16( ddlMonths.SelectedValue);

Storing Selected value in session
C#
Session["MonthValue"] = ddlMonths.SelectedValue;

Assigning the Month to the Dropdown
C#
ddlMonths.SelectedValue = Session["MonthValue"].ToString();
 
Share this answer
 
Comments
AlwzLearning 23-May-16 1:28am    
OK i tried this but it binds the dropdown back to original months. so what I am doing is if(!ispostback)
binddropdown
if (session has value)
setting dropdown .

Is this correct ? I will try again and will update you accordingly . Thank you for help
Karthik_Mahalingam 23-May-16 1:34am    
yes you have to bind in

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Globalization.DateTimeFormatInfo dateTimeInfo = new System.Globalization.DateTimeFormatInfo();
for (int i = 1; i < 13; i++)
{
string monthName = dateTimeInfo.GetMonthName(i);
ddlMonths.Items.Add(new ListItem(monthName, i.ToString()));
}

}
}
AlwzLearning 23-May-16 20:16pm    
Work Thank you. I marked solution as accepted.
Karthik_Mahalingam 23-May-16 23:03pm    
Thanks shreelekha

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