I am working In c# and inserting my data into an Access database. It runs properly but crashes when I try to insert data, any idea why?
public partial class StudentInfo : Form
{
private OleDbConnection myCon;
public StudentInfo()
{
InitializeComponent();
myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\database program\database program\Students.mdb");
}
private void InsertBtn_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into StudentInfo(Rollno,SName,SFather,SAdress) Values ('"+ Rollnotb.Text+"','"+nametb.Text+"','"+fathertb.Text+"','"+adresstb.Text+"')";
cmd.Connection=myCon;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
}
The problem can be that you are inserting all fields as
textvalues and maybe some are defined as numeric in the MS Access table (rollNo?)Also, get used to using paremeters in your queries: