G
Guest
I am having trouble with DBNull's from my SQL server. I am
building/converting a asset web app from Access and my db has nulls on
various fields . I have tried searching newsgroups and tried to get some
code and some understanding . I am a newbie to vb but my code is below, so
can anybody let me know what I am doing wrong to stop this error " Cast from
type 'DBNull' to type 'String' is not valid."
--------------------------------------------------------------------------------
Public Function NullHelper(ByVal obj As Object) As String
If IsDBNull(obj) Then Return String.Empty
Return obj.ToString()
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Public Function GetAsset(ByVal assetNo As String, ByRef adapter As
SqlDataAdapter) As DataSet
'connect to the database
Dim connection As New SqlConnection(Global.dbstring)
connection.Open()
'set up the SQL command object
Dim command As New SqlCommand("SELECT * FROM Computers WHERE
asset='" & assetNo & "'", connection)
' Retreive the data
adapter = New SqlDataAdapter(command)
' add a command builder
Dim builder As New SqlCommandBuilder(adapter)
'Fill the dataset
Dim asset As New DataSet
adapter.Fill(asset)
' disconnect
connection.Close()
' return asset details
Return asset
End Function
Public Function GetAsset(ByVal AssetNo As String) As DataSet
' Call the other Version of the method
Dim adapter As SqlDataAdapter
Dim asset As DataSet = GetAsset(AssetNo, adapter)
'Dispose of the Adapter
adapter.Dispose()
'Return asset details
Return asset
End Function
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsearch.Click
BindData(txtAssetSearch.Text)
End Sub
'BindData - Bind the data to the controls
'Overloads Overrides Public Function ToString() As String, BindData
(ByVal AssetNo As String)
Public Sub BindData(ByVal AssetNo As String)
'set lable.text
lblEditingId.Text = AssetNo
'get the blue_Number
Dim assetDataset As DataSet = GetAsset(AssetNo)
'If the Asset was found....
If assetDataset.Tables(0).Rows.Count > 0 Then
'....Reset the label Text
lblproblem.Text = ""
btnSaveChanges.Visible = True
'Update the controls
Dim Asset As DataRow = assetDataset.Tables(0).Rows(0)
txtInstallDate.Text = NullHelper(Asset("installation_date"))
txtnetname.Text = Asset("network_name") & ""
txtSerial.Text = Asset("serial_number") & ""
txtModel.Text = Asset("model") & ""
txtPlatform.Text = Asset("platform") & ""
txtMac.Text = Asset("hardware_address") & ""
txtProcessor.Text = Asset("processor_speed") & ""
txtRam.Text = Asset("ram") & ""
txtHD.Text = Asset("hard_disk_size") & ""
txtMake.Text = Asset("make") & ""
txtOS.Text = Asset("os_desc") & ""
txtKitType.Text = Asset("kit_type") & ""
txtDept.Text = Asset("department") & ""
txtBuilding.Text = Asset("building") & ""
txtFloor.Text = Asset("floor") & ""
txtMonitorAsset.Text = Asset("monitor_Asset") & ""
txtMonitorSize.Text = Asset("monitor_size") & ""
txtMonitorMake.Text = Asset("monitor_make") & ""
txtMonitorModel.Text = Asset("monitor_model") & ""
txtMonitorSerial.Text = Asset("monitor_serial_no") & ""
'txtInstallDate.Text = Asset("installation_date") & ""
'txtDateMod = Asset("date_modified") & ""
txtcomments.Text = Asset("other_info")
Else
' ....Otherwise set an error message
lblproblem.Text = "The Asset Number' " & AssetNo & _
"' does not exist"
btnSaveChanges.Visible = False
'Update the controls
txtnetname.Text = ""
txtSerial.Text = ""
txtModel.Text = ""
txtPlatform.Text = ""
txtMac.Text = ""
txtProcessor.Text = ""
txtRam.Text = ""
txtHD.Text = ""
txtMake.Text = ""
txtOS.Text = ""
txtKitType.Text = ""
txtDept.Text = ""
txtBuilding.Text = ""
txtFloor.Text = ""
txtMonitorAsset.Text = ""
txtMonitorSize.Text = ""
txtMonitorMake.Text = ""
txtMonitorModel.Text = ""
txtMonitorSerial.Text = ""
txtInstallDate.Text = ""
'txtDateMod.Text = ""
txtcomments.Text = ""
End If
End Sub
building/converting a asset web app from Access and my db has nulls on
various fields . I have tried searching newsgroups and tried to get some
code and some understanding . I am a newbie to vb but my code is below, so
can anybody let me know what I am doing wrong to stop this error " Cast from
type 'DBNull' to type 'String' is not valid."
--------------------------------------------------------------------------------
Public Function NullHelper(ByVal obj As Object) As String
If IsDBNull(obj) Then Return String.Empty
Return obj.ToString()
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Public Function GetAsset(ByVal assetNo As String, ByRef adapter As
SqlDataAdapter) As DataSet
'connect to the database
Dim connection As New SqlConnection(Global.dbstring)
connection.Open()
'set up the SQL command object
Dim command As New SqlCommand("SELECT * FROM Computers WHERE
asset='" & assetNo & "'", connection)
' Retreive the data
adapter = New SqlDataAdapter(command)
' add a command builder
Dim builder As New SqlCommandBuilder(adapter)
'Fill the dataset
Dim asset As New DataSet
adapter.Fill(asset)
' disconnect
connection.Close()
' return asset details
Return asset
End Function
Public Function GetAsset(ByVal AssetNo As String) As DataSet
' Call the other Version of the method
Dim adapter As SqlDataAdapter
Dim asset As DataSet = GetAsset(AssetNo, adapter)
'Dispose of the Adapter
adapter.Dispose()
'Return asset details
Return asset
End Function
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsearch.Click
BindData(txtAssetSearch.Text)
End Sub
'BindData - Bind the data to the controls
'Overloads Overrides Public Function ToString() As String, BindData
(ByVal AssetNo As String)
Public Sub BindData(ByVal AssetNo As String)
'set lable.text
lblEditingId.Text = AssetNo
'get the blue_Number
Dim assetDataset As DataSet = GetAsset(AssetNo)
'If the Asset was found....
If assetDataset.Tables(0).Rows.Count > 0 Then
'....Reset the label Text
lblproblem.Text = ""
btnSaveChanges.Visible = True
'Update the controls
Dim Asset As DataRow = assetDataset.Tables(0).Rows(0)
txtInstallDate.Text = NullHelper(Asset("installation_date"))
txtnetname.Text = Asset("network_name") & ""
txtSerial.Text = Asset("serial_number") & ""
txtModel.Text = Asset("model") & ""
txtPlatform.Text = Asset("platform") & ""
txtMac.Text = Asset("hardware_address") & ""
txtProcessor.Text = Asset("processor_speed") & ""
txtRam.Text = Asset("ram") & ""
txtHD.Text = Asset("hard_disk_size") & ""
txtMake.Text = Asset("make") & ""
txtOS.Text = Asset("os_desc") & ""
txtKitType.Text = Asset("kit_type") & ""
txtDept.Text = Asset("department") & ""
txtBuilding.Text = Asset("building") & ""
txtFloor.Text = Asset("floor") & ""
txtMonitorAsset.Text = Asset("monitor_Asset") & ""
txtMonitorSize.Text = Asset("monitor_size") & ""
txtMonitorMake.Text = Asset("monitor_make") & ""
txtMonitorModel.Text = Asset("monitor_model") & ""
txtMonitorSerial.Text = Asset("monitor_serial_no") & ""
'txtInstallDate.Text = Asset("installation_date") & ""
'txtDateMod = Asset("date_modified") & ""
txtcomments.Text = Asset("other_info")
Else
' ....Otherwise set an error message
lblproblem.Text = "The Asset Number' " & AssetNo & _
"' does not exist"
btnSaveChanges.Visible = False
'Update the controls
txtnetname.Text = ""
txtSerial.Text = ""
txtModel.Text = ""
txtPlatform.Text = ""
txtMac.Text = ""
txtProcessor.Text = ""
txtRam.Text = ""
txtHD.Text = ""
txtMake.Text = ""
txtOS.Text = ""
txtKitType.Text = ""
txtDept.Text = ""
txtBuilding.Text = ""
txtFloor.Text = ""
txtMonitorAsset.Text = ""
txtMonitorSize.Text = ""
txtMonitorMake.Text = ""
txtMonitorModel.Text = ""
txtMonitorSerial.Text = ""
txtInstallDate.Text = ""
'txtDateMod.Text = ""
txtcomments.Text = ""
End If
End Sub