Solved

SQL fill textboxes VB.NET

Posted on 2014-03-27
6
686 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 62

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 62

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now