Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] ASP.net using C# and MySQL

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Resolved [RESOLVED] [2.0] ASP.net using C# and MySQL

    I can't figure it out why this code only adds new row but can't save the information in the mysql database.

    Code:
    private void AddParam(MySqlCommand cmd, params string[] cols)
        {
            foreach (string col in cols)
            {
                cmd.Parameters.Add("@" + col, MySqlDbType.Text, 0, col);
            }
        }
    Code:
        public int currRec = 0;
    Code:
        protected void saveButton_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost; Database=DB; user id=userdb; password=pass; persist security info=true;");
            string sql = "select * from tblUserAccount";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
    
            DataSet ds = new DataSet();
    
            da.Fill(ds, "tblUserAccount");
            DataRow dr = ds.Tables[0].NewRow();
            ds.Tables[0].Rows.Add(dr);
    
            da.InsertCommand = conn.CreateCommand();
            da.InsertCommand.CommandText = "Insert Into tblUserAccount (fname, lname, usernames, passwords, AccountType, priv) " +
                "Values (@fname, @lname, @usernames, @passwords, @accounttype, @priv)";
            AddParam(da.InsertCommand, "fname", "lname", "usernames", "passwords", "accounttype", "priv");
            
            dr.BeginEdit();
            dr["fname"] = txtFname.Text;
            dr["lname"] = txtLname.Text;
            dr["usernames"] = txtUsername.Text;
            dr["passwords"] = txtPassword1.Text;
            dr["accounttype"] = cboAccountType.Text;
            dr["priv"] = cboPriv.Text;
            dr.EndEdit();
    
            
    
            da.Update(ds, "tblUserAccount");
            ds.AcceptChanges();
    
            conn.Close();
            conn.Dispose();
    
        }

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] ASP.net using C# and MySQL

    Because you have not specified an UpdateCommand for your DataAdapter.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: [2.0] ASP.net using C# and MySQL

    Instead of doing this:

    Code:
    private void AddParam(MySqlCommand cmd, params string[] cols)
        {
            foreach (string col in cols)
            {
                cmd.Parameters.Add("@" + col, MySqlDbType.Text, 0, col);
            }
        }


    Code:
    da.InsertCommand.CommandText = "Insert Into tblUserAccount (fname, lname, usernames, passwords, AccountType, priv) " +
                "Values (@fname, @lname, @usernames, @passwords, @accounttype, @priv)";
            AddParam(da.InsertCommand, "fname", "lname", "usernames", "passwords", "accounttype", "priv");

    I tried to change it to:


    Code:
    private void AddParam(MySqlCommand cmd, params string[] cols)
        {
            foreach (string col in cols)
            {
                cmd.Parameters.Add("?" + col, MySqlDbType.Text, 0, col);
            }
        }
    Code:
    da.InsertCommand.CommandText = "Insert Into tblUserAccount (fname, lname, usernames, passwords, AccountType, priv) " +
                "Values (?fname, ?lname, ?usernames, ?passwords, ?accounttype, ?priv)";
            AddParam(da.InsertCommand, "fname", "lname", "usernames", "passwords", "accounttype", "priv");

Posting Permissions

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



Click Here to Expand Forum to Full Width