Im having a little bit of trouble with an update statement.
if any one could take a look at it and help me with what im doing wrong that would be great.

private void button6_Click(object sender, EventArgs e)
       // Button6 is OK
        {

            ///////If Null/////
            if ((textBox1.Text.Length == 0) || (textBox2.Text.Length == 0))
            {
                MessageBox.Show("Fields cannot be empty");
                return;
            }
            //
            // Make sure there are no duplicates
            DataSet ds = new DataSet();
            DBconnect db = new DBconnect();
            string Emp = new Department('K', textBox2.Text).SetSql();
            ds = db.GetDataSet(Emp);
            DataTable dt = ds.Tables[0];
            //
           if (dt.Rows.Count > 0)
           {
                MessageBox.Show("Duplicate Record");
           }
            else
            {
                // Connect to Database 
                OleDbConnection connect = new OleDbConnection();    
                connect = new DBconnect().DbConn("");

                string UpdateStatement = "UPDATE Department "
                    + "SET [Department] = ?, [Description] = ? "
                    + "WHERE Department = '" + textBox2.Text + "'";


                OleDbCommand UpdateCommand = new OleDbCommand(UpdateStatement, connect);

                UpdateCommand.Parameters.Add("[Department]", OleDbType.Char).Value = textBox2.Text;
                UpdateCommand.Parameters.Add("[Description]", OleDbType.Char).Value = textBox1.Text;

                connect.Open();

                int count = UpdateCommand.ExecuteNonQuery();
                connect.Close();



                this.Close();
            }
        }

it has no errors it just doesnt update the database or the datagrid.

Recommended Answers

All 4 Replies

When you execute this Update, in order to see the updated data, you have to re-run the SELECT statement that you use for your GridView.

Btw, if you're not 100% sure that the query that is being created is correct, try and debug the application and then take the value of your UpdateStatement and execute it on access.

Hope i helped. :)

When you execute this Update, in order to see the updated data, you have to re-run the SELECT statement that you use for your GridView.

Btw, if you're not 100% sure that the query that is being created is correct, try and debug the application and then take the value of your UpdateStatement and execute it on access.

Hope i helped. :)

i do that when the form closes and i have access to the grid view again.
but the problem is that the data change isnt showing in access either. so even if i did put the select statemtent after, it wouldnt show anything because, by not showing the changes in either the data grid or access, its saying im not making changes.

i just need to know what i need to change or add to make the changes at least show up in access. cause after that i can figure out the rest.

Check this article (<<snip>>) for details on how to connect to SQL Server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries (SELECT, INSERT, UPDATE, DELETE), calling stored procedures, pass parameters etc.

Try changing your parameter names from:

UpdateCommand.Parameters.Add("[Department]", OleDbType.Char).Value = textBox2.Text;                
UpdateCommand.Parameters.Add("[Description]", OleDbType.Char).Value = textBox1.Text;

to

UpdateCommand.Parameters.Add("@Department", OleDbType.Char).Value = textBox2.Text;                
UpdateCommand.Parameters.Add("@Description", OleDbType.Char).Value = textBox1.Text;

I dont think your supposed to use the field name as the parameter name

Be a part of the DaniWeb community

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