当我从下拉列表中选择时,如何从表中获取ID [英] How Do I Get Id From A Table When I Make Selection From Dropdownlist
问题描述
我在ajax modalpopupExtender中有一个下拉列表。我从表中填写下拉列表然后我想在下拉列表中获取所选项目的相应ID?
##我试过这个
{
i have a dropdownlist in ajax modalpopupExtender. i fill the dropdown from a table then i am trying to get the respective id of selected item in dropdownlist??
## i have try this one
{
string select =从表格中选择Id,其中colName =' msgstr+ ddlAddDistrict.SelectedItem.Text +';
;
sda.Fill(dt);
if(dt.Rows.Count> 0)
{
int id = int.Parse(dt.Rows [0] [0] .ToString());
Response.Write(< script> alert('+ id +' )< / script>);
}
}
##我也试过这一个
string id = ddlAddDistrict.DataValueField;
一旦尝试过这个
string select = "select Id from table where colName='" + ddlAddDistrict.SelectedItem.Text + "'";
SqlDataAdapter sda = new SqlDataAdapter(select, DatabaseConnection.CreateConnection());
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
int id = int.Parse(dt.Rows[0][0].ToString());
Response.Write("<script>alert('" + id + "')</script>");
}
}
## i also tried this one
string id = ddlAddDistrict.DataValueField;
once tried this one
string MyQuery = string.Format("select Id from tableName where colName='{0}'", dropdownlsit.SelectedItem.Text);
SqlConnection con = DatabaseConnection.CreateConnection();
SqlCommand cmd = new SqlCommand(MyQuery, con);
string MyId;
MyId = cmd.ExecuteScalar() as string;
推荐答案
感谢所有人。我已经按照以下方式解决了我的问题
当我在设置下拉列表DataVAlueField属性时填写下拉列表。然后在代码中我没有正确访问
public void FillDropdownlist(){
string select = 从表名中选择col1,col2;
DataTable dt = SelectData(select);
if(dt.rows.count> 0){
ddlAddDistrict.DataSource = dt;
ddlAddDistrict.DataTextField =col1;
ddlAddDistrict.DataValueField =id;
ddlAddDistrict.DataBind();
}
}
后来我检索这样的id
string q =从tablename中选择id,其中col1 ='+ ddlAddDistrict.SelectedValue +';
sqlcommand cmd = new sqlcommand(q,connect());
string id = cmd.ExecuteScalar()。tostring();
谢谢你
al0t of thanks to you all people. i have fix my problem in the following way
when i have filled the dropdownlist at the time i used to set the dropdownlist DataVAlueField property. then in the code i did not accessed correctly
public void FillDropdownlist(){
string select = "select col1, col2 from tablename";
DataTable dt = SelectData(select);
if(dt.rows.count > 0){
ddlAddDistrict.DataSource = dt;
ddlAddDistrict.DataTextField = "col1";
ddlAddDistrict.DataValueField = "id";
ddlAddDistrict.DataBind();
}
}
later i retrieved the id in this way
string q = "select id from tablename where col1='"+ddlAddDistrict.SelectedValue+"'";
sqlcommand cmd = new sqlcommand(q, connect());
string id= cmd.ExecuteScalar().tostring();
thank u
这篇关于当我从下拉列表中选择时,如何从表中获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!