Click here to Skip to main content
15,918,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 = "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
XML
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;
Posted
Comments
[no name] 5-Sep-15 5:07am    
Are you getting dropdownlist selected value in your page? Secondly please provide aspx code for ajaxmodalpopup with dropdownlist.
F-ES Sitecore 5-Sep-15 8:00am    
I don't really follow what you're doing, but ids aren't included in form posts so your server code might not have access to them.

1 solution

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
 
Share this answer
 
Comments
Richard Deeming 15-Sep-15 12:42pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

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