I have a very difficult task, that I think exceeds my level of knowledge of databases. I am only learning it for a week, so I require help.
I currently have an old database that has tables for requests and customers. This information is currently displayed in ASP.NET
What I was required to do, was to connect a new database for users into this system, while keeping the tables for the requests in the old database. Thus it is using 2 databases. I have set it up so, when a user creates a new request, in the request tables it saves it with his userID from the new database, but when the table is being displayed, it uses this new userID to find the user information from the old customers table. Thus I ended up with this:
Sub BindDataGrid()
cmdoledb = New SqlCommand("SELECT r.customerid, r.RequestID, R.RequestDate, R.RequestDescription, a.AssignedTo, r.Urgency, r.ExCompletionDate from tbl_requests r, tbl_assignedto a where r.statusid = 1 and r.assignedtoid= a.assignedtoid ", objConn)
objConn.Open()
Dim rdr As SqlDataReader
Dim myItem As ListItem = New ListItem()
rdr = cmdoledb.ExecuteReader(0)
While rdr.Read()
myItem = New ListItem()
myItem.Value = rdr.GetInt32(0)
cmddb = New SqlCommand("SELECT SESFirstName + ' ' + SESLastName As fullname from SESLogin where SESLoginID = @customerID", objConnect)
cmddb.Parameters.AddWithValue("@customerID", myItem.Value)
objConnect.Open()
Dim anotherAdapter As New SqlDataAdapter(cmddb)
Dim dss As New DataSet
anotherAdapter.Fill(dss)
dgrdUsers.DataSource = dss
objConnect.Close()
End While
Dim myAdapter As New SqlDataAdapter(cmdoledb)
Dim ds As New DataSet
myAdapter.Fill(ds)
dgrdUsers.DataSource = ds
dgrdUsers.DataBind()
objConn.Close()
End Sub
Currently it displays nothing, and gives out an error for myItem.Value = rdr.GetInt32(0) being a Null. I went through it step by step and it loads customerids normaly into the myItem.Value, but when they run out, it is supposed to exit, but keeps going, thus giving an error.
This code is very silly i know, but with my knowledge, this is what I was able to come up with. Thus I require your help and advice, to fix this issue I am having.
Well, the first thing I would do is combine your two databases into one (preferably using views as replacements for you existing tables), if indeed you are using two dbs (it’s not clear from your code sample whether you really are using two dbs or simply additional tables).
You are executing you primary query twice, once for a data reader and once for a dataadapter.fill. You should do the fill first and then iterate over the resulting rows. The code sample is using a list item in oreder to contain a single integer, unless you are not showing all of your code, you should just use an integer variable.
I don’t know if it fits in with what you are doing or not, but you only need one dataset with two tables to contain the data you are retrieving.