jkershaw
July 16th, 2002, 03:16 PM
Good Day All,
I have been working with the editable datagrid. I am adding a dropdownlist to the edit state. I seem to have managed to bind data to the drop down list. Except when I pass data back to the database I am getting the following rather than the value of the select item: System.Data.DataRowView
I am using the following line to extract the value: DropDownList1.SelectedItem.Value or .Text
Here is some other code I use:
<EditItemTemplate>
<asp:DropDownList id=DropDownList1 runat="server" DataSource="<%#dsClass%>" DataTextField="txtClass" DataValueField="txtClass">
</asp:DropDownList>
</EditItemTemplate>
And:
myClassCommand.Fill(dsClass)
DropDownList1.DataSource = dsClass
DropDownList1.DataBind()
Obviously I am declaring and filling the dataset elsewhere in the code. When I view the drop down list it appears to have the correct data in it.
Also here is the code which sends the data back to the database:
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
Dim txtID as String = CStr(DataGrid1.DataKeys(e.Item.ItemIndex))
Dim txtHeading As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim txtSum As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim txtUrl As String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim txtClass As String = DropDownList1.SelectedItem.Value
Dim txtCatagory as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionString)
Dim UpdateCommand As SqlCommand = New SqlCommand()
UpdateCommand.Connection = myConnection
UpdateCommand.Parameters.Add("@txtHeading", SqlDbType.VarChar, 50).Value = txtHeading
UpdateCommand.Parameters.Add("@txtUrl", SqlDbType.VarChar, 50).Value = txtUrl
UpdateCommand.Parameters.Add("@txtClass", SqlDbType.VarChar, 50).Value = txtClass
UpdateCommand.Parameters.Add("@txtSum", SqlDbType.VarChar, 300).Value = txtSum
UpdateCommand.Parameters.Add("@RefID", SqlDbType.VarChar, 50).Value = txtId
UpdateCommand.Parameters.Add("@txtCatagory", SqlDbType.VarChar, 50).Value = txtCatagory
' execute the command
If AddingNew = True Then
UpdateCommand.CommandText = "INSERT INTO tblticker(txtHeading, txtSum, txtUrl, txtClass, txtCatagory) VALUES (@txtHeading, @txtSum, @txtUrl, @txtClass, @txtCatagory)"
Else
UpdateCommand.CommandText = "UPDATE tblTicker SET txtHeading = @txtHeading, txtUrl = @txtUrl, txtClass = @txtClass, txtSum = @txtSum, txtCatagory = @txtCatagory WHERE RefID = @RefID"
End If
Cheers,
John
I have been working with the editable datagrid. I am adding a dropdownlist to the edit state. I seem to have managed to bind data to the drop down list. Except when I pass data back to the database I am getting the following rather than the value of the select item: System.Data.DataRowView
I am using the following line to extract the value: DropDownList1.SelectedItem.Value or .Text
Here is some other code I use:
<EditItemTemplate>
<asp:DropDownList id=DropDownList1 runat="server" DataSource="<%#dsClass%>" DataTextField="txtClass" DataValueField="txtClass">
</asp:DropDownList>
</EditItemTemplate>
And:
myClassCommand.Fill(dsClass)
DropDownList1.DataSource = dsClass
DropDownList1.DataBind()
Obviously I am declaring and filling the dataset elsewhere in the code. When I view the drop down list it appears to have the correct data in it.
Also here is the code which sends the data back to the database:
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
Dim txtID as String = CStr(DataGrid1.DataKeys(e.Item.ItemIndex))
Dim txtHeading As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim txtSum As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim txtUrl As String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim txtClass As String = DropDownList1.SelectedItem.Value
Dim txtCatagory as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionString)
Dim UpdateCommand As SqlCommand = New SqlCommand()
UpdateCommand.Connection = myConnection
UpdateCommand.Parameters.Add("@txtHeading", SqlDbType.VarChar, 50).Value = txtHeading
UpdateCommand.Parameters.Add("@txtUrl", SqlDbType.VarChar, 50).Value = txtUrl
UpdateCommand.Parameters.Add("@txtClass", SqlDbType.VarChar, 50).Value = txtClass
UpdateCommand.Parameters.Add("@txtSum", SqlDbType.VarChar, 300).Value = txtSum
UpdateCommand.Parameters.Add("@RefID", SqlDbType.VarChar, 50).Value = txtId
UpdateCommand.Parameters.Add("@txtCatagory", SqlDbType.VarChar, 50).Value = txtCatagory
' execute the command
If AddingNew = True Then
UpdateCommand.CommandText = "INSERT INTO tblticker(txtHeading, txtSum, txtUrl, txtClass, txtCatagory) VALUES (@txtHeading, @txtSum, @txtUrl, @txtClass, @txtCatagory)"
Else
UpdateCommand.CommandText = "UPDATE tblTicker SET txtHeading = @txtHeading, txtUrl = @txtUrl, txtClass = @txtClass, txtSum = @txtSum, txtCatagory = @txtCatagory WHERE RefID = @RefID"
End If
Cheers,
John