Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this the right way to do this?

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
Is this okay to do, or should I do it a different way

Dim dbconn1 As SqlConnection = New SqlConnection("server=AM1ST_FS1;database=HRINFO;uid=sa;")
Dim selectCMD1 As SqlCommand = New SqlCommand()
selectCMD1.CommandText = "Select * from employeeinfo where loginID = '" & employee.SelectedItem.Value & "'"
selectCMD1.Connection = dbconn1
selectCMD1.CommandTimeout = 30
dbconn1.Open()
Dim empDA As SqlDataAdapter = New SqlDataAdapter
empDA.SelectCommand = selectCMD1
Dim empDS As DataSet = New DataSet
empDA.Fill(empDS, "employeeinfo")
dbconn1.Close()

selectCMD1.CommandText = "Select * from timeoffbene where loginID = '" & employee.SelectedItem.Value & "'"
selectCMD1.Connection = dbconn1
selectCMD1.CommandTimeout = 30
dbconn1.Open()
empDA = New SqlDataAdapter
empDA.SelectCommand = selectCMD1
empDS = New DataSet
empDA.Fill(empDS, "timeoffbene")
dbconn1.Close()

selectCMD1.CommandText = "Select * from familyinfo where loginID = '" & employee.SelectedItem.Value & "'"
selectCMD1.Connection = dbconn1
selectCMD1.CommandTimeout = 30
dbconn1.Open()
empDA = New SqlDataAdapter
empDA.SelectCommand = selectCMD1
empDS = New DataSet
empDA.Fill(empDS, "familyinfo")
dbconn1.Close()

selectCMD1.CommandText = "Select * from moneyinfo where loginID = '" & employee.SelectedItem.Value & "'"
selectCMD1.Connection = dbconn1
selectCMD1.CommandTimeout = 30
dbconn1.Open()
empDA = New SqlDataAdapter
empDA.SelectCommand = selectCMD1
empDS = New DataSet
empDA.Fill(empDS, "moneyinfo")
dbconn1.Close()

I had one before, and now when I do this when I load the page I get this error

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 252: dbconn1.Close()
Line 253:
Line 254: loginID1.text = empDS.tables("employeeinfo").Rows(0).Item("loginID")

 
You are creating a new dataset every time. You should use the same dataset without creating a new one. Take all the

empDS = New DataSet

out besides the first one.

What is happening you are creating always a new object with the same name, therefore you lose all the tables you had put in.

hth Daren J. Lahey
Just another computer guy...
 
another good trick you would want to get used to is to use the try, catch, finally statements to enhance your error handling:
[c#]

try
{
command1.connection.Open();
dataadapter1.fill(dataset1,"myTable");
}
catch (exception ex)
{
//error handling
throw ex;
}
finally
{
command1.connection.Close();
}

hth
Daren J. Lahey
Just another computer guy...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top