Solved

SQL fill textboxes VB.NET

Posted on 2014-03-27
6
708 Views
Last Modified: 2014-03-31
I have a form that populates textboxes based on sql connection string.  I use datareader to do it.  The problem I am having is if the sql table has a null value in one of the columns, it will error out.  The only fix I have found that works is using On Error Resume Next at the top of the code, which I do not like using.  I have searched online for about a month to find an answer to this, and none of the solutions I have found works.  If you have any suggestions, please let me know.  Thanks in advance.  Below is the code I am using.
        Dim connection As SqlConnection = New SqlConnection("Data Source='" + sqlseverlocation + "';Initial Catalog='" + sqltable + "';Persist Security Info=True;User ID='" + sqluname + "';Password='" + sqlpass + "'")
        Dim command As SqlCommand = New SqlCommand("Select Item, AGENCY, Unit, Location, [Activity Code], [Case #], Month, Day, Year, Received, Dispatched, InRoute, Arrived, ToHosp, ArrvHosp, Completed, Remarks, AddNum, SeneMileage, HospMileage, TotalMiles, Operator, [Enter Info], Address FROM COMMUNICATIONS WHERE AGENCY='" + fdname + "' AND Item='" + itemsub1 + "';", connection)

        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        If reader.HasRows Then
            Do While reader.Read()
                txtCasenumber.Text = reader.GetInt32(0)
                txtUnit.Text = reader.GetString(2)
                txtCaseNum.Text = reader.GetString(5)
                txtStreet.Text = reader.GetString(3)
                txtMonth.Text = reader.GetString(6)
                txtDay.Text = reader.GetString(7)
                txtYear.Text = reader.GetString(8)
                txtreceived.Text = reader.GetDateTime(9)
                txtDispatched.Text = reader.GetDateTime(10)
                txtArrived.Text = reader.GetDateTime(12)
                txtToHosp.Text = reader.GetDateTime(13)
                txtToHosp.Text = reader.GetDateTime(13)
                txtArrivedHosp.Text = reader.GetDateTime(14)
                txtCompleted.Text = reader.GetDateTime(15)
                txtActivityCode.Text = reader.GetString(4)
                txtSceneMil.Text = reader.GetInt32(18)
                txtHospMil.Text = reader.GetInt32(19)
                txtTotalMil.Text = reader.GetInt32(20)
                txtoperator.Text = reader.GetString(21)
                txtDetails.Text = reader.GetString(16)
                txtAddress.Text = reader.GetString(23)
            Loop
        Else

        End If
        connection.Close()

Open in new window

0
Comment
Question by:russell12
6 Comments
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 39958700
use Convert.ToString(reader[1]) so on..
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39958703
You cannot set a Textbox Text which is a string to null.

Therefore you have to  check each value before assigning to text box. For example:

If If reader.GetString(23) is  NOT null then
 
txtAddress.Text = reader.GetString(23)

End if
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 39959149
Hi russell12;

You can use the Visual Basic .Net If operator to return a non Nothing value. In the following statement the if operator will return the value of reader.GetString(2) unless it evaluates to Nothing and if it does evaluates to Nothing it will return an empty string. You will need to do this on all lines that could return a Nothing value.

txtUnit.Text = If(reader.GetString(2), String.Empty)
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 39959749
You have to be careful about that one.

Fernando code is OK for your display, but maybe not for your update.

You risk storing back empty strings in the table instead of a Null value when you update the data with the changes. This might end up causing problems down the line if your application or other applications perform operations where they need to detect null values.

If this is the case, you will have to make sure to pass DBNull.Value instead of String.Empty to your UPDATE for the TextBoxes that will be empty.
0
 
LVL 2

Author Closing Comment

by:russell12
ID: 39965785
Thank you both for your help.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39966565
Not a problem, always glad to help if I can.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question