Click here to Skip to main content
14,830,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Frnds,
i have a dropdown control.while displaying it i have to show the Item name...but while saving in the Database i have to save Item Code which is integer not the Item Name.
how can it be done.

Regards,
aamir
Posted

Use below code:

C#
if(ddlItemCode.SelectedValue != "")
   int itemCode = Convert.Int32(ddlItemCode.SelectedValue);


Pass itemCode to the database while saving.
   
v2
hi vani,
but while filing the Dropdown i am just getting the Name of item and not the Id.
i have used sqldatareader();to fill the Dropdown like below
C#
SqlDataReader dr = command.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string p;

                        if (dr[0].ToString() == null || dr[0].ToString() == "";)
                        {
                        }
                        else
                        {
                            p = dr[0].ToString();
                            drp.Items.Add(p);

                        }
                    }
                }
   
Comments
Vani Kulkarni 29-Jun-12 0:13am
   
How are you binding the dropdown? Does it have a only ItemName?
plz get item name and item code from dropdown ie
dr[0] is item name
and dr[1] is item code
C#
SqlDataReader dr = command.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string p;
 
                        if (dr[0].ToString() == null || dr[0].ToString() == "";)
                        {
                        }
                        else
                        {
                          
                       //drp.Items.Add(new ListItem("text","value") it is syntax now in your case add following


                drp.Items.Add(new ListItem(dr[0].toString(),dr[1].toString())
                        }
                    }
                }



from this code you save itemname as text and itemcode as value of dropdown

now you can get value of dropdown using following Code
int itemCode = Convert.Int32(drp.SelectedValue);


and save itemcode in your databse
may be this will help you
   
v2
for binding the dropdown list through dataset
C#
dropdownlist1.items.clear();
dropdownlist1.datasource=ds;
dropdownlist1.datatextfield="Itemname";
dropdownlist1.datavaluefield="ItemCode";
dropdownlist1.databind();


for saving the value field into database
C#
string sql= insert into (columnname) values ("+dropdownlist1.selectedvalue+");


Mark it answer if you get your solution

Thanks
Ashish
   
Comments
aamir07 29-Jun-12 0:33am
   
hi ashish,
i got the above query solved, while saving in DB i get the code...but now i want, on select of the Gridview row to show the ItemName not the code in the DropDownList control.
AshishChaudha 29-Jun-12 1:09am
   
You are showing the itemname in your gridview!! right..for showing the itemname while selecting through the gridview you have to bind the itemcode in gridview too...did you have binded the itemcode too in your gridview?? if yes then follow the code

dropdownlist1.selectedvalue = gridview1.selectedrow.cells[yourcolumnno of gridview].text;

//for example
dropdownlist1.selectedvalue = gridview1.selectedrow.cells[2].text;

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