Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't populate the value of the city in a record from my database into a dropdownlist upon clicking the edit button from the gridview...

I have this button beside my GridView:
ASP.NET
<asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="ButtonEdit" runat="server" Text="Edit" causesvalidation = "false"
                    OnCommand="EditDetails" CommandArgument='<%# Eval("TableID").ToString() %>' CommandName="ButtonCommandEdit"
                     />
                </ItemTemplate>
        </asp:TemplateField>


And then, this: (Note that this is just EDIT button and not UPDATE button yet)


C#
string recordID = ""; //to be used in ***
protected void EditDetails(object sender, CommandEventArgs e)
        {
            recordID = e.CommandArgument.ToString(); //to set value of ***
            GridViewUserInfoTable.Enabled = false;
            ButtonUpdate.Visible = true;
            ButtonCancel.Visible = true;
            ButtonSubmit.Visible = false;
            var connectionString = "server=localhost;uid=root;" + "pwd=xxxxx;database=xxxxx;";
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var query = "select * from table where tableID= " + recordID; //using the value of ***
                using (var command = new MySqlCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {

                        while (reader.Read())
                        {
                            TextBoxfirstName.Text = (reader.GetString("firstName"));
                            TextBoxmiddleName.Text = (reader.GetString("middleName"));
                            TextBoxlastName.Text = (reader.GetString("lastName"));
                            TextBoxmobileNo.Text = (reader.GetString("mobileNo"));
                            TextBoxemailAddress.Text = (reader.GetString("emailAddress"));
                            TextBoxregistrationDate.Text = reader.GetDateTime("registrationDate").ToString("yyyy-MM-dd");
                            TextBoxstreet.Text = (reader.GetString("street"));
                            DropDownListCity.SelectedValue = (reader.GetString("city")); 
//i believe i need to change something here in DropDownLists
                            DropDownListRegion.SelectedValue = (reader.GetString("region"));
                            TextBoxcountry.Text = (reader.GetString("country"));
                        }
                    }
                }
            }
            if (Page.IsPostBack)
            {
                //nothing
            }
        }


Everything populates except for the drop down lists... Can someone please help me with this? All responses will be highly appreciated. Thanks in advance!
Posted
Comments
Ramug10 6-Mar-14 4:32am    
are you getting any exception?
DropDownListCity.SelectedValue = (reader.GetString("city"));
here city means cityid or cityname??
[no name] 6-Mar-14 4:35am    
I'm not getting any... I'm wondering why data has been populated in the textboxes whilst none appears on DDLists...
Ramug10 6-Mar-14 4:36am    
DropDownListCity.SelectedValue = (reader.GetString("city"));
here city means cityid or cityname??

are you bind DropDownListCity in page load?
[no name] 6-Mar-14 4:44am    
when I call
DropDownListCity.SelectedValue = (reader.GetString("city"));
I mean to call the city Name.

I didn't bind DropDownListCity in page load, only DropDownListRegion so that upon changing the region, the DropDownList City will show the cities included for that region
Ramug10 6-Mar-14 4:47am    
okey..after binding data to city dropdown only you hitting this edit grid right?

try the below code

DropDownListCity.SelectedItem.Text = (reader.GetString("city")); 
 
Share this answer
 
Friend,

1- First you Bind the DropDownListCity dropdownlist then you can check & assigned selected value.
2- Use IsPostBack on your page load.

or

DropDownListCity.Items.Add(new ListItem(reader.GetString("city")));

or if value required

DropDownListCity.Items.Add(new ListItem("Text","value");
 
Share this answer
 

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