Cast from type 'DBNull' to type 'String' is not valid error

R

Rob

Trying to add an insert button Sub btnAddRow_Click event
for adding a row to the datagrid and dataset back to SQL.
Had it working per the documentation, but have since added
a drop down list object and update capability that should
be able to submit a null value. Update works. SQL table
allows Nulls.

After clicking Add row button, get an error in Sub
DataGrid1_ItemDataBound:

"Cast from type 'DBNull' to type 'String' is not valid"

Code for small app below. Help would be most
appreciated. Thank you, Rob
----
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'RW - Session state for insert function, then
fills the dataset with stored procedure
If IsPostBack Then
DsAttachmentDtls1 = CType(Session
("SessionAttachmentDtls"), dsAttachmentDtls)
Else
SqlDataAdapter1.Fill(DsAttachmentDtls1)
Session("SessionAttachmentDtls") =
DsAttachmentDtls1
DataGrid1.DataBind()
End If
End Sub

' RW - Insert new row into Dataset session state, then
puts the grid into Edit/Update at row 0. After save
button pressed, data actually written to database
Private Sub btnAddRow_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnAddRow.Click
Dim dr As DataRow =
DsAttachmentDtls1.tblAttachmentDtls.NewRow
dr(3) = EmployeeID
dr(5) = Now
DsAttachmentDtls1.tblAttachmentDtls.Rows.InsertAt
(dr, 0)
Session("SessionAttachmentDtls") =
DsAttachmentDtls1
DataGrid1.EditItemIndex = 0
DataGrid1.DataBind()
End Sub
' RW - Set grid to edit mode.
Private Sub DataGrid1_EditCommand(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

' RW - DataGrid update session state changes in the
dataset to the stored procedure.
Private Sub DataGrid1_UpdateCommand(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.UpdateCommand
Dim key As String = DataGrid1.DataKeys
(e.Item.ItemIndex).ToString()
Dim tb As TextBox
Dim Attach_Type As String
Dim STS_DATE_ATTACH As Date
Dim DESCR As String
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"),
DropDownList)
Attach_Type = ddl.SelectedValue

tb = CType(e.Item.Cells(6).Controls(0), TextBox)
STS_DATE_ATTACH = tb.Text

tb = CType(e.Item.Cells(7).Controls(0), TextBox)
DESCR = tb.Text

Dim r As dsAttachmentDtls.tblAttachmentDtlsRow
r =
DsAttachmentDtls1.tblAttachmentDtls.FindByATTACHID(key)

r.ATTACH_TYPE() = Attach_Type
r.STS_DATE_ATTACH() = STS_DATE_ATTACH
r.DESCR() = DESCR

SqlDataAdapter1.Update(DsAttachmentDtls1)
DataGrid1.EditItemIndex = -1
'Refresh the dataset from the database
DsAttachmentDtls1.Clear()
SqlDataAdapter1.Fill(DsAttachmentDtls1)
'Save the refreshed dataset in Session state agin
Session("SessionAttachmentDtls") =
DsAttachmentDtls1
DataGrid1.DataBind()
End Sub

' RW - Takes DataGrid out of Edit mode, back to read-
only
Private Sub DataGrid1_CancelCommand(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub

'RW - Function to fill drop downlist in Edit mode of
DataGrid. Calls SQL command for tblAttachType values.
Public Function GetAttachType() As DataTable
Dim dtAttachType As DataTable = New DataTable
If Application("tblAttachType") Is Nothing Then
Dim dr As DataRow
Dim dc As New DataColumn("ATTACH_TYPE")
dtAttachType.Columns.Add(dc)
Me.SqlConnection1.Open()
Dim dreader As SqlClient.SqlDataReader = _
Me.SqlAttachType.ExecuteReader()
While dreader.Read()
dr = dtAttachType.NewRow()
dr(0) = dreader(0)
dtAttachType.Rows.Add(dr)
End While
Me.SqlConnection1.Close()
Else
dtAttachType = CType(Application
("tblAttachType"), DataTable)
End If
Return dtAttachType
End Function

'RW - defaults drop down list of AttachType to the
value from the row in edit mode.
Private Sub DataGrid1_ItemDataBound(ByVal sender As
Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If Not IsNothing(e.Item.DataItem) Then
If e.Item.ItemType = ListItemType.EditItem Then
Dim drv As DataRowView = CType
(e.Item.DataItem, DataRowView)
Dim currentAttachType As String = CType(drv
("ATTACH_TYPE"), String)
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl
("DropDownList1"), DropDownList)
ddl.SelectedIndex = ddl.Items.IndexOf
(ddl.Items.FindByText(currentAttachType))
End If
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top