Solved

SQL fill textboxes VB.NET

Posted on 2014-03-27
6
694 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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