Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how do i write changes back to db

$
0
0
SqlConnection con = new SqlConnection(connect);


            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataAdapter mail;
            DataTable mailtable;
            mailtable = new DataTable();


            try
            {
                con.Open();
                mail = new SqlDataAdapter(cmd);

                // this will query your database and return the result to your datatable
                mail.Fill(mailtable);
                this.dataGridView1.Visible = true;
                dataGridView1.DataSource = mailtable;


                con.Close();
            }
            catch (Exception m)
            {
                MessageBox.Show(m.Message);
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

I have this code that queries a sql db and outputs to a grid view

i would like to be able to update the grid view and then write the changes back to the db so  have this code for a button

private void button3_Click(object sender, EventArgs e)
        {


            try
            {
                string connect;
                if (Properties.Settings.Default.Intergated == false)
                {
                    string p = StringEncryptor.Decrypt(Properties.Settings.Default.Pass, Properties.Settings.Default.key);
                    connect = "Data Source=" + Properties.Settings.Default.Server + ";Initial Catalog=" + Properties.Settings.Default.DB + ";Integrated Security=false;UID=" + Properties.Settings.Default.User + ";password=" + p + ";";
                }

                else
                {
                    connect = "Data Source=" + Properties.Settings.Default.Server + ";Initial Catalog=" + Properties.Settings.Default.DB + ";Integrated Security=true;";
                }

                SqlConnection con = new SqlConnection(connect);


                SqlCommand cmd = new SqlCommand(lastquery, con);
                SqlDataAdapter mail;
                DataTable mailtable;
                mailtable = new DataTable();


               
                    con.Open();
                    mail = new SqlDataAdapter(cmd);

                    // this will query your database and return the result to your datatable
                    mail.Fill(mailtable);



                    mail.Update(what goes in here??);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

i am not sure of the syntax i need to get the updated gridview data into the

mail.Update(what goes in here??);

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>