Amien90
asked on
why is my dropdownlist in gridview not updated?
the new productname is written to the database, but databind will not show the new productname. See code
Thanks in advanced
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>
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()
ASKER
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?
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!!
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!!
ASKER
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.
"You do need to do a 'update datasource' before doing a databind. "
how can i do this? whats the syntax, cant find it.
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!!
dropdown.Datasource = (your datasource object);
//set data text, and data value here.
dropdown.Databind();
Mark as answer if helped.
Enjoy!!
ASKER
This :
Dim ddlProducts As DropDownList = GridView1.Rows(RowIndex).C ells(13).F indControl ("ddlProdu cts")
ddlProducts.DataSource = AccessDataSourceProducts
ddlProducts.DataBind()
gives me this error:
Both DataSource and DataSourceID are defined on 'ddlProducts'. Remove one definition.
Dim ddlProducts As DropDownList = GridView1.Rows(RowIndex).C
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"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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()
ASKER