strSQL = "SELECT * FROM User_Reg WHERE User_ID='" + UserId + "'";
DataTable dataTablerepeaterUserList = null;
dataTablerepeaterUserList =objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
foreach (DataRow dr in dataTablerepeaterUserList.Rows)
{
ddlTitle.SelectedItem.Text = dr["Title"].ToString();
}
<asp:DropDownList ID="ddlTitle" runat="server" CssClass="csstextbox" >
<asp:ListItem>Mr.</asp:ListItem>
<asp:ListItem>Mrs.</asp:ListItem>
<asp:ListItem>Miss.</asp:ListItem>
<asp:ListItem>Dr.</asp:ListItem>
</asp:DropDownList>
If, suppose, dr[“Title”]=Dr. Then it is overwrite Mr. with Dr. and got result like:
| Dr. |
| Mrs. |
| Miss.|
| Dr. |
I am passing User_ID from View_User.aspx to Create_User.aspx for edit user purpose using querystring. Id getting proper, showing all correct fields in Create_User.aspx. All controls get correct data properly. But when I populate title of current user then it overwrite with first item of DropDownList. (I have only problem with DropDownList it is overwritten).
The problem with your code, is that you are just overwriting the
.textproperty of the firstListItemin theDropDownList.What you want to do is find the existing
ListItemthat you want, and modifies theSelectedIndexproperty of theDropDownListso that the proper item is selected. This should work for you:As a side note, you should not concatenate strings in your SQL, like in your example. This leaves you open to SQL Injection attacks. Instead you should use parameters, like this:
And then add “@User_ID” as a parameter to wherever you’re executing your query.