Solved

why is my dropdownlist in gridview not updated?

Posted on 2012-04-02
8
273 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
  • 5
  • 3
8 Comments
 

Author Comment

by:Amien90
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Amien90
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:dhawalseth
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now