Solved

SQL fill textboxes VB.NET

Posted on 2014-03-27
6
681 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

9 Experts available now in Live!

Get 1:1 Help Now