I am new to asp.net although i had a good experience working with ASP.
I m connecting to 2 different databases, i.e Database A and Database B. Then Selecting fields from Database1.TableA trying to JOIN to Database2.TableB. I then bind to Datagrid.
I used the folowing code which works fine but only if for every field in Database1.TableA there will be an IDENTICAL joined filed in Database2.TableB.
Errors occur:
1)if there are null values within one table,
2)different datatypes between the tables,
3)and most importantly if there are redundant values existing in one table but not the other.
I dont know if the following code is the best way to do it but all im trying is to get a bloody join between two databases and display the result to a Datagrid. I know in ASP things were a lot simpler by looping through recordsets using return values from on query to another and so on creating as many connections as you like. This is excactly what i need to achieve... Anyway code as folows PLEASE HELP:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Dataset1 As New DataSet
Dim Dataset2 As New DataSet
Dim Datatable As New DataTable("newdata")
Dim DataColumn1 As New DataColumn("firstname", GetType(String))
Dim DataColumn2 As New DataColumn("surname", GetType(String))
Dim DataColumn3 As New DataColumn("id", GetType(Integer))
Dim sqlConn As SqlConnection
Dim datAdapter As SqlDataAdapter
Dim i As Integer
Dataset1 = New DataSet
sqlconn = New SqlConnection("server=(local);database=MSc_Project_DB;Integrated Security=SSPI;Connect Timeout=3600")
datAdapter = New SqlDataAdapter("select * from tableA", sqlconn)
sqlconn.Open()
datAdapter.Fill(Dataset1, "TableA")
sqlconn.Close()
sqlconn = New SqlConnection("server=(local);database=MSc_Project_DB;Integrated Security=SSPI;Connect Timeout=3600")
datAdapter = New SqlDataAdapter("select * from tableB", sqlconn)
sqlconn.Open()
datAdapter.Fill(Dataset1, "TableB")
sqlconn.Close()
Dataset1.Relations.Add("TableA_TableB", Dataset1.Tables("TableA").Columns("id"), Dataset1.Tables("TableB").Columns("TableA_ID"))
Datatable.Columns.Add(DataColumn1)
Datatable.Columns.Add(DataColumn2)
Datatable.Columns.Add(DataColumn3)
Datatable.AcceptChanges()
Dataset2.Tables.Add(Datatable)
Dim dta As DataTable = Dataset2.Tables("newdata")
Dim dt_a As DataTable = Dataset1.Tables("tablea")
For i = 0 To Dataset1.Tables("tablea").Rows.Count - 1
Dim dr As DataRow = dta.NewRow
dr("firstname") = dt_a.Rows(i)("name")
Dim dr2() As DataRow = dt_a.Rows(i).GetChildRows("tablea_tableb")
dr("surname") = dr2(0)("surname")
dr("id") = dr2(0)("id")
Datatable.Rows.Add(dr)
dr = Nothing
dr2 = Nothing
Next
'DataGrid1 is the Control on the form
DataGrid1.DataSource = Dataset2.Tables("newdata")
DataGrid1.DataBind()
sqlconn.Close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Feel free to show me a different way if possible to do this modyfing my code completely if needed.
All i need to do is simply join 2 database tables accross different databases and put the result to a datagrid (in this case) or another data control if you think is simpler or better way of doing it.
p.s i used Local server in this example and some dummy field names i.e firstname, surname e.t.c but in real life scenaro this will not be the case...
I m connecting to 2 different databases, i.e Database A and Database B. Then Selecting fields from Database1.TableA trying to JOIN to Database2.TableB. I then bind to Datagrid.
I used the folowing code which works fine but only if for every field in Database1.TableA there will be an IDENTICAL joined filed in Database2.TableB.
Errors occur:
1)if there are null values within one table,
2)different datatypes between the tables,
3)and most importantly if there are redundant values existing in one table but not the other.
I dont know if the following code is the best way to do it but all im trying is to get a bloody join between two databases and display the result to a Datagrid. I know in ASP things were a lot simpler by looping through recordsets using return values from on query to another and so on creating as many connections as you like. This is excactly what i need to achieve... Anyway code as folows PLEASE HELP:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Dataset1 As New DataSet
Dim Dataset2 As New DataSet
Dim Datatable As New DataTable("newdata")
Dim DataColumn1 As New DataColumn("firstname", GetType(String))
Dim DataColumn2 As New DataColumn("surname", GetType(String))
Dim DataColumn3 As New DataColumn("id", GetType(Integer))
Dim sqlConn As SqlConnection
Dim datAdapter As SqlDataAdapter
Dim i As Integer
Dataset1 = New DataSet
sqlconn = New SqlConnection("server=(local);database=MSc_Project_DB;Integrated Security=SSPI;Connect Timeout=3600")
datAdapter = New SqlDataAdapter("select * from tableA", sqlconn)
sqlconn.Open()
datAdapter.Fill(Dataset1, "TableA")
sqlconn.Close()
sqlconn = New SqlConnection("server=(local);database=MSc_Project_DB;Integrated Security=SSPI;Connect Timeout=3600")
datAdapter = New SqlDataAdapter("select * from tableB", sqlconn)
sqlconn.Open()
datAdapter.Fill(Dataset1, "TableB")
sqlconn.Close()
Dataset1.Relations.Add("TableA_TableB", Dataset1.Tables("TableA").Columns("id"), Dataset1.Tables("TableB").Columns("TableA_ID"))
Datatable.Columns.Add(DataColumn1)
Datatable.Columns.Add(DataColumn2)
Datatable.Columns.Add(DataColumn3)
Datatable.AcceptChanges()
Dataset2.Tables.Add(Datatable)
Dim dta As DataTable = Dataset2.Tables("newdata")
Dim dt_a As DataTable = Dataset1.Tables("tablea")
For i = 0 To Dataset1.Tables("tablea").Rows.Count - 1
Dim dr As DataRow = dta.NewRow
dr("firstname") = dt_a.Rows(i)("name")
Dim dr2() As DataRow = dt_a.Rows(i).GetChildRows("tablea_tableb")
dr("surname") = dr2(0)("surname")
dr("id") = dr2(0)("id")
Datatable.Rows.Add(dr)
dr = Nothing
dr2 = Nothing
Next
'DataGrid1 is the Control on the form
DataGrid1.DataSource = Dataset2.Tables("newdata")
DataGrid1.DataBind()
sqlconn.Close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Feel free to show me a different way if possible to do this modyfing my code completely if needed.
All i need to do is simply join 2 database tables accross different databases and put the result to a datagrid (in this case) or another data control if you think is simpler or better way of doing it.
p.s i used Local server in this example and some dummy field names i.e firstname, surname e.t.c but in real life scenaro this will not be the case...