I am doing a project in Visual C# and trying to insert data into MS Access database but whenever I press the insert button in the C# Windows From a message box appears and its written "Microsoft Access Database Engine". I have installed Microsoft Access Database engine but still the problem is not solved. Can anyone suggest me how to overcome this problem.
subratabanerjee 0 Newbie Poster
Momerath 1,327 Nearly a Senior Poster Featured Poster
Post your code
subratabanerjee 0 Newbie Poster
OleDbConnection conn = new OleDbConnection();
private void btn_ADDNEWSTUDENT_Click(object sender, EventArgs e)
{
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Subrata\Documents\Visual Studio 2010\Projects
\CollegeStudentManagementSystem\CollegeStudentManagementSystem\bin\studentinfo1.mdb.accdb";
string USN = student_USN1.Text;
string MYNAME = student_NAME1.Text;
string FNAME = student_FATHERNAME1.Text;
string MNAME = student_MOTHERNAME1.Text;
string ADDRESS = student_ADDRESS1.Text;
string CITY = student_CITY1.Text;
string STATE = student_STATE1.Text;
string PINCODE = student_PINCODE1.Text;
conn.Open();
OleDbCommand cmmd = new OleDbCommand("INSERT into student(USN, SNAME, FATHERNAME, MOTHERNAME, ADDRESS, CITY, STATE, PINCODE)" + "VALUES(@USN,
@MYNAME, @FNAME, @MNAME, @ADDRESS, @CITY, @STATE, @PINCODE)", conn);
if (conn.State == ConnectionState.Open)
{
cmmd.Parameters.Add("@USN", OleDbType.Char, 20);
cmmd.Parameters.Add("@SNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@FATHERNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@MOTHERNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@ADDRESS", OleDbType.Char, 20);
cmmd.Parameters.Add("@CITY", OleDbType.Char, 20);
cmmd.Parameters.Add("@STATE", OleDbType.Char, 20);
cmmd.Parameters.Add("@PINCODE", OleDbType.Char, 20);
cmmd.Parameters["@USN"].Value = USN;
cmmd.Parameters["@SNAME"].Value = MYNAME; ;
cmmd.Parameters["@FATHERNAME"].Value = FNAME;
cmmd.Parameters["@MOTHERNAME"].Value = MNAME;
cmmd.Parameters["@ADDRESS"].Value = ADDRESS;
cmmd.Parameters["@CITY"].Value = CITY;
cmmd.Parameters["@STATE"].Value = STATE;
cmmd.Parameters["@PINCODE"].Value = PINCODE;
try
{
cmmd.ExecuteNonQuery();
MessageBox.Show("DATA ADDED");
conn.Close();
}
catch (OleDbException expe)
{
MessageBox.Show(expe.Source);
}
}
else
{
MessageBox.Show("CON FAILED");
}
}
subratabanerjee 0 Newbie Poster
Here is the entire code:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace CollegeStudentManagementSystem
{
public partial class CSMS : Form
{
public CSMS()
{
InitializeComponent();
}
OleDbConnection conn = new OleDbConnection();
private void tabPage3_Click(object sender, EventArgs e)
{
}
private void btn_ADDNEWSTUDENT_Click(object sender, EventArgs e)
{
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Subrata\Documents\Visual Studio 2010\Projects
\CollegeStudentManagementSystem\CollegeStudentManagementSystem\bin\studentinfo1.mdb.accdb";
string USN = student_USN1.Text;
string MYNAME = student_NAME1.Text;
string FNAME = student_FATHERNAME1.Text;
string MNAME = student_MOTHERNAME1.Text;
string ADDRESS = student_ADDRESS1.Text;
string CITY = student_CITY1.Text;
string STATE = student_STATE1.Text;
string PINCODE = student_PINCODE1.Text;
conn.Open();
OleDbCommand cmmd = new OleDbCommand("INSERT into student(USN, SNAME, FATHERNAME, MOTHERNAME, ADDRESS, CITY, STATE, PINCODE)" + "VALUES(@USN,
@MYNAME, @FNAME, @MNAME, @ADDRESS, @CITY, @STATE, @PINCODE)", conn);
if (conn.State == ConnectionState.Open)
{
cmmd.Parameters.Add("@USN", OleDbType.Char, 20);
cmmd.Parameters.Add("@SNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@FATHERNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@MOTHERNAME", OleDbType.Char, 20);
cmmd.Parameters.Add("@ADDRESS", OleDbType.Char, 20);
cmmd.Parameters.Add("@CITY", OleDbType.Char, 20);
cmmd.Parameters.Add("@STATE", OleDbType.Char, 20);
cmmd.Parameters.Add("@PINCODE", OleDbType.Char, 20);
cmmd.Parameters["@USN"].Value = USN;
cmmd.Parameters["@SNAME"].Value = MYNAME; ;
cmmd.Parameters["@FATHERNAME"].Value = FNAME;
cmmd.Parameters["@MOTHERNAME"].Value = MNAME;
cmmd.Parameters["@ADDRESS"].Value = ADDRESS;
cmmd.Parameters["@CITY"].Value = CITY;
cmmd.Parameters["@STATE"].Value = STATE;
cmmd.Parameters["@PINCODE"].Value = PINCODE;
try
{
cmmd.ExecuteNonQuery();
MessageBox.Show("DATA ADDED");
conn.Close();
}
catch (OleDbException expe)
{
MessageBox.Show(expe.Source);
}
}
else
{
MessageBox.Show("CON FAILED");
}
}
}
}
Edited by subratabanerjee because: n/a
hag++ 24 Junior Poster
Well for one it seems like you are catching an OleDbException and showing a MessageBox with the Source property in it. Calling the Source property alone doesn't really tell you much about why the exception is being thrown. Try putting the Message property in the message box instead and let us know what that says. Since I don't use access I can't really test your code locally.
subratabanerjee 0 Newbie Poster
Thanks a lot hag++
I found the error and solved it.
jnbradl 0 Newbie Poster
How was this issued solved? What was the error? thanks
Edit: Just saw where this user has been gone for a while. I was creating a project to learn how to add form data into MS Access and was using this as a base. Can anyone suggest a website/video that will help with this. I just want to be able to take data from textboxs and add it to an Access table with a button click. Thank you in advance. Do I need to start a new thread?
Edited by jnbradl because: Edit
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.