Reputation: 49
Here i am adding 5 datas to the table(Registration), and it contains 6 six columns among that the ID is primary KEY indentity. But i am getting an exception that no value for one or more required parameters at "myCommand.ExecuteReader();" while trying to insert the other data columns.
public void SubmitClick(Object sender, EventArgs e )
{
insertdata(FN.Text,LN.Text,DOB.Text,mailid.Text,username.Text);
}
public void insertdata(string fname, string sname, string dob ,string email ,string uname)
{
//Connection string for the datbase
string database = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\atchutharamkj\documents\visual studio 2010\Projects\WebApplication1\WebApplication1\App_Data\Database3.accdb;Persist Security Info=True";
OleDbConnection myConn = new OleDbConnection(database);
string queryStr = @"Insert into Registration Values (fname , sname, dob , email, uname)";
OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
myCommand.Connection.Open();
myCommand.ExecuteReader();
myCommand.Connection.Close();
}
Upvotes: 0
Views: 1242
Reputation: 17600
Your query is wrong and this way you are not passing anything to it.
Use parametrized queries like that:
public void SubmitClick(Object sender, EventArgs e )
{
insertdata(FN.Text,LN.Text,DOB.Text,mailid.Text,username.Text);
}
public void insertdata(string fname, string sname, string dob ,string email ,string uname)
{
string database = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\atchutharamkj\documents\visual studio 2010\Projects\WebApplication1\WebApplication1\App_Data\Database3.accdb;Persist Security Info=True";
OleDbConnection myConn = new OleDbConnection(database);
string queryStr = @"Insert into Registration(FirstName,LastName,DOB,[E-Mail],Username ) Values (?, ?, ?, ?, ?)";
OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
myCommand.Parameters.AddWithValue("@fname", fname);
myCommand.Parameters.AddWithValue("@sname", sname);
myCommand.Parameters.AddWithValue("@dob", dob);
myCommand.Parameters.AddWithValue("@email", email);
myCommand.Parameters.AddWithValue("@uname", uname);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
More about passing parameters to OleDB see MSDN.
Upvotes: 4