Closed Thread
Results 1 to 3 of 3

Thread: Update records problem

  1. #1
    desir is offline Newbie
    Join Date
    May 2009
    Posts
    5
    Rep Power
    0

    Update records problem

    Hello
    I am trying to update record in a database. I can see the changes in the datagrid, but they don't get saved into the database.. The 3 textboxes show the values of the currently selected row in the datagrid. So i press the Change button to make the textboxes active, change the text and then click the Save button.
    Here's how the form looks like: img529.imageshack.us/my.php?image=45903634.jpg

    Code:
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private SqlConnection conn;
            private string connStr;
            SqlDataAdapter da;
            DataSet ds;
            bool b;
    
            public Form1()
            {
                InitializeComponent();
                connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\dbOne.mdf" + ";Integrated Security=True;Connect Timeout=30;User Instance=True";
                conn = new SqlConnection(connStr);
                conn.Open();
                string selectStr = "select * from people";
                da = new SqlDataAdapter(selectStr, conn);
                ds=new DataSet();
                da.Fill(ds,"people");
    
                da.InsertCommand = new SqlCommand("insert into people(name, age) values (@name, @age)", conn);
                da.InsertCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name");
                da.InsertCommand.Parameters.Add("@age",SqlDbType.Int,5,"age");
    
                da.UpdateCommand = new SqlCommand("update people set name=@name, age=@age where id=@id",conn);
                da.UpdateCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name");
                da.UpdateCommand.Parameters.Add("@age",SqlDbType.Int,5,"age");
                da.UpdateCommand.Parameters.Add("@id",SqlDbType.Int,5,"id");
    
                da.DeleteCommand = new SqlCommand("delete from people where id=@id",conn);
                da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int,5,"id");
    
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dataGridView1.DataSource = ds.Tables["people"];
                txtId.DataBindings.Add("Text",ds.Tables["people"],"id");
                txtName.DataBindings.Add("Text", ds.Tables["people"], "name");
                txtAge.DataBindings.Add("Text", ds.Tables["people"], "age");
    
                txtId.Enabled = false; txtName.Enabled = false; txtAge.Enabled = false; 
                btnSave.Enabled = false; btnCancel.Enabled = false;
                conn.Close();
            }
    
            private void btnChange_Click(object sender, EventArgs e)
            {
                b = false;
                txtName.Enabled = true; txtAge.Enabled = true;
                btnNew.Enabled = false; btnDelete.Enabled = false; btnSave.Enabled = true; btnCancel.Enabled = true;
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {            
                if (b) // insert new line
                {
                    DataRow newLine = ds.Tables["people"].NewRow();
                    newLine["name"] = txtName.Text;
                    newLine["age"] = txtAge.Text;
                    ds.Tables["people"].Rows.Add(newLine);
                }
                da.Update(ds,"people");
            }
    
            private void btnNew_Click(object sender, EventArgs e)
            {
                b = true;
                int i = ds.Tables["people"].Rows.Count;
                dataGridView1.Rows[i].Selected = true;
    
                txtId.Text = ""; txtName.Text = ""; txtAge.Text = "";
                txtName.Enabled = true; txtAge.Enabled = true;
                btnSave.Enabled = true; btnCancel.Enabled = true;
            }
    
            private void btnDelete_Click(object sender, EventArgs e)
            {
                int i = int.Parse(dataGridView1.CurrentRow.Index.ToString());
                ds.Tables["people"].Rows[i].Delete();
                da.Update(ds,"people");
            }
    
    
        }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: Update records problem

    I have a feeling that your problem is asynchronous, fatal metadata desynchronisation.
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  4. #3
    TommiTMX is offline Newbie
    Join Date
    May 2009
    Posts
    5
    Rep Power
    0

    Re: Update records problem

    Are you running the database from within Visual C#? If so then I believe that the database wont save any changes made when debugging the software (you will actually need to Publish the program for the database to work.

    (I could be wrong I'm still learning lol)

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. login records problem
    By capoon in forum PHP Development
    Replies: 2
    Last Post: 09-28-2011, 06:17 PM
  2. select and update database records
    By mutago in forum PHP Development
    Replies: 6
    Last Post: 05-26-2011, 01:27 PM
  3. Problem with update in ADO.NET
    By Abbath1349 in forum C# Programming
    Replies: 1
    Last Post: 05-21-2011, 12:37 PM
  4. login update problem
    By mutago in forum Java Help
    Replies: 3
    Last Post: 03-24-2011, 05:42 AM
  5. Problem with UPDATE using submit button
    By bajerocks in forum PHP Development
    Replies: 2
    Last Post: 07-01-2010, 03:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts