Problems displaying data on asp.net webform

klfarr

New member
Joined
Jul 13, 2005
Messages
2
Programming Experience
Beginner
Problems displaying data on asp.net webform [RESOLVED]

I am creating a web-based inventory system to track computers and other equipment in an office. This first page I am creating is a "Details" page that returns information on one record (one computer).

At the bottom of this post is the basic code I have created so far. The subs FillMasterData and FillMachinesData fill each label on the web page with data from the datareaders.

I have a problem:

1) Null values in the DB create runtime errors - "Cast from type 'DBNull' to type 'String' is not valid." I can get around the runtime errors if I use the following "if...then" code around EACH data fill in the FillMasterData and FillMachinesData subs:

VB.NET:
		If IsDBNull(dtrMachines("PrinterDuplex")) = False Then
			lblPrinterDuplex.Text = dtrMachines("PrinterDuplex")
		End If

This seems very clumsy. Is there an easier way to display this data w/out having to manually populate each label in the code and validate the non-Null status?

Am I approaching this code in the wrong manner, or should I just suck it up and write the "if...then" code around each and every data population line?

Below is the code for the project thus far:

VB.NET:
	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		

		Dim dtrMaster As System.Data.SqlClient.SqlDataReader	 'Create DataReader object for 'Master' table
		Dim dtrMachines As System.Data.SqlClient.SqlDataReader	 'Create DataReader object for 'Machines' table
		Dim SerialNumber As String								 'Create SerialNumber string

		SerialNumber = Request.QueryString("SerialNumber")		 'Assign SerialNumber value from URL to string
		If IsNothing(SerialNumber) Then							 'If SerialNumber string is empty...
		 Label1.Text = "Error: No QueryString Serial Number Text Passed in URL" 'Error message
		 'Response.Redirect("Search.aspx")				 'Redirect if empty
			Return
		End If

	 SqlCommand1.Parameters("@SerialNumber").Value = SerialNumber	'Set SerialNumber param in SQL Command object for performing SQL Query
	 SqlConnection1.Open()									 'Open connection
		dtrMaster = SqlCommand1.ExecuteReader					 'Fill DataReader object
		If dtrMaster.Read Then									 'If dtrMaster is filled, then fill the following labels with data
		 FillMasterData(dtrMaster)							 'Run FillMasterData subroutine, pass dtrMaster datareader object
		End If

		SqlCommand2.Parameters("@ModelName").Value = dtrMaster("ModelName")
	 SqlConnection2.Open()									 'Open connection
		dtrMachines = SqlCommand2.ExecuteReader
		If dtrMachines.Read Then
		 FillMachinesData(dtrMachines)						 'Run FillMachinesData subroutine, pass dtrMachines datareader object
		End If

		'Assign subheader text (needs both data sources)
		lblSubHeader.Text = dtrMachines("Manufacturer") & " " & dtrMachines("EquipType") & " named " & dtrMaster("NetworkName") & " assigned to user " & dtrMaster("UserName")

	 SqlConnection1.Close()									 'Close connection
	 SqlConnection2.Close()									 'Close connection

	End Sub

	Sub FillMasterData(ByVal dtrMaster)
		'This subroutine is made to separate the data filling steps from the Page_Load sub.
		'This makes things easier to read. This sub is only for the 'Master' table variables
		'in the Inventory DB

		lblSerialNumber.Text() = dtrMaster("SerialNumber")
		lblMACAddress.Text() = dtrMaster("MACAddress")
		lblIPAddress.Text() = dtrMaster("IPAddress")
		lblNetworkName.Text() = dtrMaster("NetworkName")
		lblUserName.Text() = dtrMaster("UserName")
		lblLocation.Text() = dtrMaster("Location")
		lblUserGroup.Text() = dtrMaster("UserGroup")
		lblLocationGroup.Text() = dtrMaster("LocationGroup")
		lblPurchaseOrder.Text() = dtrMaster("PurchaseOrder")
		lblPurchaseDate.Text() = dtrMaster("PurchaseDate")
		lblWarrantyValidThru.Text() = dtrMaster("WarrantyValidThru")
		lblWarrantyInfo.Text() = dtrMaster("WarrantyInfo")
		lblComments.Text() = dtrMaster("Comments")
		lblModelName.Text() = dtrMaster("ModelName")
	End Sub

	Sub FillMachinesData(ByVal dtrMachines)
		'This subroutine is made to separate the data filling steps from the Page_Load sub.
		'This makes things easier to read. This sub is only for the 'Machines' table variables
		'in the Inventory DB
		lblEquipType.Text = dtrMachines("EquipType")
		lblManufacturer.Text = dtrMachines("Manufacturer")
		lblMFRModelName.Text = dtrMachines("MFRModelName")
		lblCPU.Text = dtrMachines("CPU")
		lblRAM.Text = dtrMachines("RAM")
		lblHD.Text = dtrMachines("HD")
		lblPrimaryDisplayModelName.Text = dtrMachines("PrimaryDisplayModelName")
		lblSecondaryGFXCardName.Text = dtrMachines("SecondaryGFXCardName")
		lblSecondaryDisplayModelName.Text = dtrMachines("SecondaryDisplayModelName")
		lblOpticalDevice.Text = dtrMachines("OpticalDevice")
		lblFloppyDisk.Text = dtrMachines("FloppyDisk")
		lblOSVersion.Text = dtrMachines("OSVersion")
		lblPrinterDuplex.Text = dtrMachines("PrinterDuplex")
		lblPrinterNumTrays.Text = dtrMachines("PrinterNumTrays")
		lblPrinterEnvelopeFeeder.Text = dtrMachines("PrinterEnvelopeFeeder")
		lblPrinterNetworked.Text = dtrMachines("PrinterNetworked")
		lblScannerAutoFeed.Text = dtrMachines("ScannerAutoFeed")
		lblHardwareAccessories.Text = dtrMachines("HardwareAccessories")
	End Sub
 
Last edited:
This code will turn your null values into empty strings:
VB.NET:
lblPrinterDuplex.Text = dtrMachines("PrinterDuplex").ToString()
Remember, every object has a ToString method.
 
Back
Top