asp.net - How to get ID against selected value of Dropdownlist C# -
i have 2 question
how id against selected value of dropdownlist binded db?
then how insert id in other table ?
kindly explain code..
thanx in advance
to id code
string query = "select id table-1 name=" + dropdwonlist.selectedvalue; sqlcommand cmd = new sqlcommand(query, con); sqldatareader dr = cmd.executereader(); string getid = dr[0].tostring();
dropdownlist binding code
string query = "select id, name table-1"; sqlconnection con = new sqlconnection(constr); sqldataadapter da = new sqldataadapter(query, con); datatable dt = new datatable(); da.fill(dt); dropdwonlist.datasource = dt; dropdwonlist.datatextfield = "name"; dropdwonlist.datavaluefield = "id"; dropdwonlist.databind(); dropdwonlist.items.insert(0, new listitem("--select name--"));
1) string id = dropdwonlist.selectedvalue;
2)
to insert table use query:
string id = dropdwonlist.selectedvalue; using (sqlconnection sql = new sqlconnection("your connection string")) { sqlcommand cmd = new sqlcommand(); string query = @"insert table2(column1) values(" + id + ")"; cmd.commandtext = query; cmd.commandtype = commandtype.text; cmd.connection = sql; sql.open(); cmd.executenonquery(); sql.close(); }
you should way because ensure closing connection after using it.
Comments
Post a Comment