Solved

why is my dropdownlist in gridview not updated?

Posted on 2012-04-02
8
279 Views
Last Modified: 2012-04-03
the new productname is written to the database, but databind will not show the new productname. See code

Thanks in advanced

<asp:TemplateField HeaderText="Product in Actie" Visible="True">
                    <ItemTemplate>
                          <asp:DropDownList ID="ddlProducts" runat="server" DataSourceID="AccessDataSourceProducts"
                        DataTextField="ProductName" DataValueField="ProductID" 
                        AppendDataBoundItems="True" Width="302px" TabIndex="9019"  
                      >
                         <asp:ListItem Text="" Value="" />
                    </asp:DropDownList>

Open in new window


  Dim txProductNameManual As TextBox = GridView1.Rows(RowIndex).Cells(14).FindControl("txProductNameManual")

            If Len(txProductNameManual.Text) > 0 Then
                Dim sqlstring_product As String = "INSERT INTO Products " _
                                                    & "(ProductName) " _
                                                    & "VALUES (@ProductName)"

                Dim cmd_product As New OleDbCommand(sqlstring_product, con)
                cmd_product.CommandType = CommandType.Text
                cmd_product.Parameters.AddWithValue("@ProductName", Strings.Trim(txProductNameManual.Text))

                cmd_product.ExecuteNonQuery()
                cmd_product.Parameters.Clear()

                Dim ddlProducts As DropDownList = GridView1.Rows(RowIndex).Cells(13).FindControl("ddlProducts")
                ddlProducts.DataBind()

Open in new window

0
Comment
Question by:Amien90
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:Amien90
ID: 37798905
i think i need to do a datasource again before the databind? can't i just execute a select again on my DataSourceID?
0
 

Author Comment

by:Amien90
ID: 37799719
or any other way to update my datasource? do i really need to create a new table, execute a select query and then a databind?
0
 
LVL 7

Expert Comment

by:dhawalseth
ID: 37799852
Dear Amien90,
You do need to do a 'update datasource' before doing a databind. Or else bind your drop down list on grid view row databound. Whenever your grid view will reload, which i believe will happen when a new entry is made in the db, the rows will be recreated and drop down will bind with the help of your code.

Mark as answer if helped.
Enjoy!!
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Amien90
ID: 37799857
Thanks for your reply.

"You do need to do a 'update datasource' before doing a databind. "

how can i do this? whats the syntax, cant find it.
0
 
LVL 7

Expert Comment

by:dhawalseth
ID: 37799907
Use your sql query used to initially populate the drop down. Replace your dropdown.databind with the code below:

dropdown.Datasource = (your datasource object);
//set data text, and data value here.
dropdown.Databind();

Mark as answer if helped.
Enjoy!!
0
 

Author Comment

by:Amien90
ID: 37799925
This :  

 Dim ddlProducts As DropDownList = GridView1.Rows(RowIndex).Cells(13).FindControl("ddlProducts")

ddlProducts.DataSource = AccessDataSourceProducts
ddlProducts.DataBind()

gives me this error:

Both DataSource and DataSourceID are defined on 'ddlProducts'.  Remove one definition.

  <asp:DropDownList ID="ddlProducts" runat="server" DataSourceID="AccessDataSourceProducts"
                        DataTextField="ProductName" DataValueField="ProductID" 
                        AppendDataBoundItems="True" Width="302px" TabIndex="9019"  

Open in new window

0
 
LVL 7

Accepted Solution

by:
dhawalseth earned 500 total points
ID: 37800097
It means you are binding the same control twice. I would recommend you remove the binding mentioned in the UI and populate your datasource through code from code behind.
The call from the UI is made when you first render your dropdown and anyway you have to bind it again after update.
Look in the code (RowDataBound event) and bind your control in there.. it is pretty similar to what will happen when you do a data binding from UI.
0
 

Author Comment

by:Amien90
ID: 37800815
Fixed!
 Dim txProductNameManual As TextBox = GridView1.Rows(RowIndex).Cells(14).FindControl("txProductNameManual")

            If Len(txProductNameManual.Text) > 0 Then
                Dim sqlstring_product As String = "INSERT INTO Products " _
                                                    & "(ProductName) " _
                                                    & "VALUES (@ProductName)"

                Dim cmd_product As New OleDbCommand(sqlstring_product, con)
                cmd_product.CommandType = CommandType.Text
                cmd_product.Parameters.AddWithValue("@ProductName", Strings.Trim(txProductNameManual.Text))

                cmd_product.ExecuteNonQuery()
                cmd_product.Parameters.Clear()

                Dim cmd As New OleDb.OleDbCommand("SELECT DISTINCT * FROM [Products] ORDER BY ProductName ", con)

                Dim ddlProducts As DropDownList = CType(GridView1.Rows(RowIndex).Cells(13).FindControl("ddlProducts"), DropDownList)

                Dim da As New OleDbDataAdapter(cmd)

                Dim ds As New DataSet()
                da.Fill(ds)

                ddlProducts.DataTextField = ds.Tables(0).Columns("ProductName").ToString()
                ddlProducts.DataValueField = ds.Tables(0).Columns("ProductID").ToString()

                ddlProducts.DataSource = ds.Tables(0)
                ddlProducts.DataBind()

                ds.Dispose()

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question