surferxo3 0 Newbie Poster
public partial class DataTrialForm : Form

    {

        private String connectionString = null;

        private SqlConnection sqlConnection = null;

        private SqlDataAdapter sqlDataAdapter = null;

        private SqlCommandBuilder sqlCommandBuilder = null;

        private DataTable dataTable = null;

        private BindingSource bindingSource = null;

        private String selectQueryString = null;



        public DataTrialForm()

        {

            InitializeComponent();

        }



        private void DataTraiForm_Load(object sender, EventArgs e)

        {

            

            sqlConnection = new SqlConnection("data source=SEZ-WS-137\\SQLEXPRESS2008;initial catalog=AttendanceData;user id=sa;password=pass");

            selectQueryString = "select top 20 * from Attendance  order by punchtime asc";



            sqlConnection.Open();



            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);

            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);



            dataTable = new DataTable();

            sqlDataAdapter.Fill(dataTable);

            bindingSource = new BindingSource();

            bindingSource.DataSource = dataTable;



            dataGridViewTrial.DataSource = bindingSource;

            

            // to hide Identity column

            dataGridViewTrial.Columns[0].Visible = false;

        }



        private void addUpadateButton_Click(object sender, EventArgs e)

        {

            try

            {

                sqlDataAdapter.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

                MessageBox.Show(exceptionObj.Message.ToString());

            }

        }



        private void deleteButton_Click(object sender, EventArgs e)

        {

            try

            {

               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);

               sqlDataAdapter.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

               MessageBox.Show(exceptionObj.Message.ToString());

            }

        }

    }

I found this code on net & I have the following problem:
In my c# project I have a database class which handles all the insert, update , delete stuff... I have a form adminpanel where the user types the desired username in the textbox and the click the search button the button uses the database class to search the data.. I have function there in the database class which returns all the the DATATABLE filled with the results of the user query... Then I bind the DATATABLE to my forms datagridview & all works well... But I want that when the user updates or adds any data in the datagrid view & when the user press the update button on the form the data should be updated directly in the database... I took help from the above code but I dont want the whole code to be pasted again on my form. My database class return the datatable... I used the binding source to the datatable and attached the binding source at the datagridview datasource... I added this line in my update button code. da.update(dataTable) but its not working... I want to know anyway how to pass this datatable to my database class so tht my code works well... I have attached the following stuff of my project so that you can understand it more clearly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.