how to store multiple DataTables into single DataSet in c#? - c#

I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.
MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
How can I do that?

DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...
If you want your tables named in the DataSet you can create your tables from the DataSet
DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1"); // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name
You can also set the TableName property of the tables if you use the first approach.

Related

Must declare the scalar variable #empname

Getting Error in Csharp :
string sq = "select EmpName,contactnumber,dob,Address from tbl_emp_details where empname = #empname";
SqlCommand cmd = new SqlCommand(sq, con);
cmd.Parameters.AddWithValue("#empname", comboBox1.SelectedText);
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
// com.Parameters.AddWithValue("#empname",Name.SelectedText);
DataTable dt = new DataTable();
adp.Fill(dt);
Name.Text = dt.Rows[0]["Empname"].ToString();
You're creating the data adapter using the original SQL and not the command (which includes the parameter) that you create.
Change
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
to
SqlDataAdapter adp = new SqlDataAdapter(cmd);

Save edits from DataGridView to Datatable

Hi i want to save edits in DataGriView to datatable , i tried that code but an error shows 'System.ArgumentOutOfRangeException: 'The index was out of range. It must not be negative and must be smaller than the size of the collection.
Parameter name: index '' help
private void button11_Click(object sender, EventArgs e)
{
con.Open();
String query = "SELECT * FROM Taom";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
dataGridView1.DataSource = dt1;
SDA.Fill(dt);
dataGridView1.Rows[2].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value)));
dataGridView1.Rows[3].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value)));
ds1.Tables.Add(dt);
con.Close();}
I changed my code to that code no errors showed after i run values on datagridview change but no changes in datatable !!!
string query = "SELECT * FROM [dbo].[Taom]";
SqlConnection conn = new SqlConnection(#"Data Source=STE-P0024818PW;Initial Catalog=test;Integrated Security=True");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Contents"] = "98"; //Before it was 10
dt.Rows[1]["Contents"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
Fill the right value inside your table and after pass the table rightly filled in your datasource, don't change this directly in the gridview like:
I try myself and it works:
private void Run()
{
string query = "SELECT * FROM dbo.[Anrufthema]";
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Anrufthema"] = "98"; //Before it was 10
dt.Rows[1]["Anrufthema"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
}
My Result, it works !

datagrid doesn't bind with datatable

I'm working on a webpage in azure. Here's a part of my code:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Off course that instead of "my connection string" there's the real one...
The problem is that the page loads but without the gridview for some reason...
Any help will be great, Thanks
EDIT: I also tried with datasets, like this:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
Try with GridView1.DataSource = ds.Tables[0];
whole code will look as follows:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

DataGridView binding not working

I am working on a winforms project and i have this following code in the Form_Load method. But it doesnt work. Can anyone help me?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sella.Properties.Settings.Database1ConnectionString1"].ConnectionString);
// A SqlCommand object is used to execute the SQL commands.
SqlCommand scmd = new SqlCommand("Select * From CustCalls", conn);
// A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
SqlDataAdapter sda = new SqlDataAdapter(scmd);
// Create and Fill a new DataSet.
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds;
Try sourcing directly to the table in the dataset:
dataGridView1.DataSource = ds.Tables[0];
SqlDataAdapter sda = new SqlDataAdapter(scmd, conn);
dataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;

How to edit datagridview using SqlCeDataAdapter?

I've a frmEdit with datagridview that's bouned to this:
string sqlqry1 = "select p_Name from Items where p_Id=" + p_Id;
using (SqlCeDataAdapter a = new SqlCeDataAdapter(sqlqry1, conn))
{
DataTable dt1 = new DataTable();
a.Fill(dt1);
dataGridView1.DataSource = dt1;
}
How to edit cells and save them back to the db?, tried using this:
using (SqlCeDataAdapter a = new SqlCeDataAdapter(sqlqry1, conn))
{
DataTable dt1 = new DataTable();
a.Fill(dt1);
dataGridView1.DataSource = dt1;
a.Update(dt1);
}
Nothing.
Is there anyway?.
Found solution:
SqlCeDataAdapter da;
SqlCeCommandBuilder cmdBuilder;
da = new SqlCeDataAdapter("select * from Items", conn);
cmdBuilder = new SqlCeCommandBuilder(da);
da.Fill(myDatabaseDataSet, "Items");
myDatabaseDataSet.Tables["Items"].Rows[0]["p_Name"] = dataGridView1.Rows[0].Cells["p_Name"].Value.ToString();
da.Update(myDatabaseDataSet, "Items");
DataTable dT;
BindingSource bS;
using (SqlCeConnection yourConnection = new SqlCeConnection("Data Source=|DataDirectory|\\YourDatabase.sdf"))
{
dT = new DataTable();
bS = new BindingSource();
string query = "SELECT * FROM table01";
SqlCeDataAdapter dA = new SqlCeDataAdapter(query, yourConnection);
SqlCeCommandBuilder cBuilder = new SqlCeCommandBuilder(dA);
dA.Fill(dT);
bS.DataSource = dT;
dgv01.DataSource = bS;
}
Resurrecting this with a working example because of this pages rank on google.

Categories