|
-
May 10th, 2007, 08:55 AM
#1
Thread Starter
Addicted Member
[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();
}
-
May 10th, 2007, 02:04 PM
#2
Re: [2.0] ASP.net using C# and MySQL
Because you have not specified an UpdateCommand for your DataAdapter.
-
May 10th, 2007, 08:53 PM
#3
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|