Filling a dataset not working
-
Hi, I need a little help. I have created an untyped dataset on my form called dataSet1 and am trying to create a parent/child relationship with a datagrid displaying the data. But I am getting a weird exception on the code:
da.Fill(dataSet1);
The exception says: "Incorrect syntax near '*'" I don't understand this exception atall. Please help me, Any reply is appreciated. P.S. here is the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace NorthwindTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { string strConnect = @"server = (local)\\SQLEXPRESS; integrated security = sspi; database = C:\\SQL\\NORTHWND.MDF"; string strQuery1 = @"SELECT \* FROM employees"; string strQuery2 = @"SELECT \* FROM orders"; string strQuery = strQuery1 + strQuery2; SqlConnection conn = new SqlConnection(strConnect); SqlDataAdapter da = new SqlDataAdapter(strQuery, conn); da.TableMappings.Add("Table", "employees"); da.TableMappings.Add("Table1", "orders"); da.Fill(dataSet1); DataRelation relation = new DataRelation("employeeorders", dataSet1.Tables\[0\].Columns\["employeeid"\], dataSet1.Tables\[1\].Columns\["employeeid"\]); dataSet1.Relations.Add(relation); dataGridView1.DataBindings.Add("datasouce", dataSet1, "employees"); } }
}
-
Hi, I need a little help. I have created an untyped dataset on my form called dataSet1 and am trying to create a parent/child relationship with a datagrid displaying the data. But I am getting a weird exception on the code:
da.Fill(dataSet1);
The exception says: "Incorrect syntax near '*'" I don't understand this exception atall. Please help me, Any reply is appreciated. P.S. here is the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace NorthwindTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { string strConnect = @"server = (local)\\SQLEXPRESS; integrated security = sspi; database = C:\\SQL\\NORTHWND.MDF"; string strQuery1 = @"SELECT \* FROM employees"; string strQuery2 = @"SELECT \* FROM orders"; string strQuery = strQuery1 + strQuery2; SqlConnection conn = new SqlConnection(strConnect); SqlDataAdapter da = new SqlDataAdapter(strQuery, conn); da.TableMappings.Add("Table", "employees"); da.TableMappings.Add("Table1", "orders"); da.Fill(dataSet1); DataRelation relation = new DataRelation("employeeorders", dataSet1.Tables\[0\].Columns\["employeeid"\], dataSet1.Tables\[1\].Columns\["employeeid"\]); dataSet1.Relations.Add(relation); dataGridView1.DataBindings.Add("datasouce", dataSet1, "employees"); } }
}
Well I'd rather use a typed dataset for this. Its makes it alot easier for u And dont combine both queries in one string. Just makea typed dataset and then just fill both tables with the TableAdapter.Fill method and add the relation to teh DatagridView hope it helps Rocky
-
Well I'd rather use a typed dataset for this. Its makes it alot easier for u And dont combine both queries in one string. Just makea typed dataset and then just fill both tables with the TableAdapter.Fill method and add the relation to teh DatagridView hope it helps Rocky
-
Ok thanks, I'll try this, but I'm just a beginner at ADO.NET so I may come back here for help if I have misunderstood what you mean.
-
Hi, I need a little help. I have created an untyped dataset on my form called dataSet1 and am trying to create a parent/child relationship with a datagrid displaying the data. But I am getting a weird exception on the code:
da.Fill(dataSet1);
The exception says: "Incorrect syntax near '*'" I don't understand this exception atall. Please help me, Any reply is appreciated. P.S. here is the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace NorthwindTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { string strConnect = @"server = (local)\\SQLEXPRESS; integrated security = sspi; database = C:\\SQL\\NORTHWND.MDF"; string strQuery1 = @"SELECT \* FROM employees"; string strQuery2 = @"SELECT \* FROM orders"; string strQuery = strQuery1 + strQuery2; SqlConnection conn = new SqlConnection(strConnect); SqlDataAdapter da = new SqlDataAdapter(strQuery, conn); da.TableMappings.Add("Table", "employees"); da.TableMappings.Add("Table1", "orders"); da.Fill(dataSet1); DataRelation relation = new DataRelation("employeeorders", dataSet1.Tables\[0\].Columns\["employeeid"\], dataSet1.Tables\[1\].Columns\["employeeid"\]); dataSet1.Relations.Add(relation); dataGridView1.DataBindings.Add("datasouce", dataSet1, "employees"); } }
}
**string strConnect = @"server = (local)\SQLEXPRESS; integrated security = sspi; database = C:\SQL\NORTHWND.MDF"; string strQuery1 = @"SELECT * FROM employees"; string strQuery2 = @"SELECT * FROM orders"; string strQuery = strQuery1 + strQuery2; SqlConnection conn = new SqlConnection(strConnect); SqlDataAdapter da = new SqlDataAdapter(strQuery, conn); **When you add the strings : strQuery = strQuery1 + strQuery2; here the strQuery becomes: "SELECT * FROM employeesSELECT * FROM orders" and you can see this is an invalid query , so it results in error. Regards,
Sushant Duggal.****