Solved

SQL fill textboxes VB.NET

Posted on 2014-03-27
6
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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