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 !
Related
I want to syncronize my database with from my datagridview. When i modify, add or remove columns from my datagridview i want to update changed rows only.
Here is my code, but this code always insert new rows each time i run it so it has duplicate values.
string conStr = "Server=servername,1433;Database=dbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
public void UpdateDatabase(DataTable dtOldList, DataTable dtNewList)
{
string selectStatement = "SELECT * FROM Customers";
System.Data.DataTable dt = new System.Data.DataTable();
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = new SqlCommand(selectStatement, conn);
SqlCommandBuilder cb = new SqlCommandBuilder(sqlDa);
dtOldList.Merge(dtNewList);
DataTable d3 = dtNewList.GetChanges();
sqlDa.Update(d3);
}
Code seems to be fine but when i click it, nothing happens.
THanks!
private void button2_Click(object sender, EventArgs e)
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand("select * from poitems", coninsert);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "poitems");
da.Update(ds, "poitems");
//DataTable dt1 = new DataTable();
//da.Fill(dt1);
//da.Update(dt1);
//dtgPo.DataSource = dt1;
}
you are not updating anything in your dataset, check below sample update code
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da, true);
DataTable dt = new DataTable();
da.Fill(dt);
//update datatable
dt.Rows[0][0] = "my changed value";
DataTable changes = dt.GetChanges();
//call update
da.Update(changes);
dt.AcceptChanges();
i have to search data from the text box in datagrid using c#
My code is
private void button_Search_Click(object sender, EventArgs e)
{
sqlcon.Open();
//DataSet ds15 = new DataSet();
DataTable dt= new DataTable();
SqlDataAdapter adpt = new SqlDataAdapter("Select ColumName from TableName where Field like '%{0}%'", comboBox_Search.Text);
adpt.Fill(dt);//datatable to catch the fields from the database
dataGridView1.DataSource = dt;
Getting error Arguement exception was unhandled
I want to search through combo box
there is no constructor match your parameters on SqlDataAdapter
SqlDataAdapter adpt = new SqlDataAdapter(string.Format("Select ColumName from TableName where Field like '%{0}%'",
comboBox_Search.Text), sqlcon);
Query every time to the database is not a preferable approach. Instead take a BindingSource object and fill the source once. Then use BindingSource.Filter property to get relevant result set and bind the result set to grid.
Have a look at this and and this link.
Also, to fix your issue you can try like this:
....
sqlcon.Open();
string query = string.Format("Select ColumName from TableName where Field like '%{0}%'", comboBox_Search.Text);
SqlCommand cm = new SqlCommand(query, sqlcon);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);//datatable to catch the fields from the database
dataGridView1.DataSource = dt;
....
How does the SqlDataAdapter know where to get the result from?
You are not initializing the constructor for your SqlDataAdapter properly. The first argument is the full select statement and the second argument is your connection string.
SqlDataAdapter adpt = new SqlDataAdapter(string.Format("Select ColumName from atm_status where Table like '%{0}%'", comboBox_Search.Text), sqlcon);
private void txt_Searchque_TextChanged(object sender, EventArgs e)
{
string connector_string = "datasource = localhost;port=3306;username=root;password=;";
MySqlConnection sqlcon = new MySqlConnection(connector_string);
sqlcon.Open();
string query = string.Format("Select * from oep.quiz where que like '%{0}%'", txt_Searchque.Text);
MySqlCommand cmd = new MySqlCommand(query, sqlcon);
MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
adpt.Fill(dt);
dataGridView1.DataSource = dt;
}
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;
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.