New to asp.net

J

JX

Trying to learn some asp dotnet and can't get an insert into MSAccess to
work. Can anyone point out my mistakes here:
Access Table is: Users with 3 columns - UserName Text(255), PasswordHash
Text(40) and salt text(10)
private bool InsertRecord(string asUserName, string asHashedPassword, string
asKey)
{
OleDbConnection conn = new OleDbConnection( DataSource );
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
try
{
conn.Open();
cmd = new OleDbCommand("INSERT INTO Users (UserName, PasswordHash, salt) " +
"VALUES (@In1, @In2, @In3)", conn);
cmd.Parameters.Add("@UserName", OleDbType.VarChar, 525 , asUserName);
cmd.Parameters.Add("@PasswordHash", OleDbType.VarChar, 40,
asHashedPassword);
cmd.Parameters.Add("@salt", OleDbType.VarChar, 10, asKey);
lblMessage.Text = "Inserting";
da.InsertCommand = cmd;
lblMessage.Text = "Done Inserting: " + In1;
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
conn.Close();
}
return true;
}
 
H

Hans Kesting

JX said:
Trying to learn some asp dotnet and can't get an insert into MSAccess
to work. Can anyone point out my mistakes here:
Access Table is: Users with 3 columns - UserName Text(255),
PasswordHash Text(40) and salt text(10)
private bool InsertRecord(string asUserName, string asHashedPassword,
string asKey)
{
OleDbConnection conn = new OleDbConnection( DataSource );
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
try
{
conn.Open();
cmd = new OleDbCommand("INSERT INTO Users (UserName, PasswordHash,
salt) " + "VALUES (@In1, @In2, @In3)", conn);
cmd.Parameters.Add("@UserName", OleDbType.VarChar, 525 , asUserName);
cmd.Parameters.Add("@PasswordHash", OleDbType.VarChar, 40,
asHashedPassword);
cmd.Parameters.Add("@salt", OleDbType.VarChar, 10, asKey);
lblMessage.Text = "Inserting";
da.InsertCommand = cmd;
lblMessage.Text = "Done Inserting: " + In1;
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
conn.Close();
}
return true;
}

1) for an INSERT you don't need a DataAdapter
2) you need assign the Connection to the Command.Connection property
3) use Command.ExecuteNonQuery to execute the query
4) maybe you want to "return false;" from the catch

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top