Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
        {
            int enrol = int.Parse(enrl.Text); 
            string fullName = (fullname.Text);
            string fathername =(fname.Text);
            string mname = (mothername.Text);
            string rel = (DropRelgn.Text);
            string gender = (dropGen.Text);             
            string eml = (email.Text);
            string occ = (fOcc.Text);
            string prvschl =(prevschool.Text);
            string addr = (address.Text);
            string facility = (hostel.Text);
             string ag = (dateTextbox.Text);
            string mobile = (mob.Text);
            string adm = (admDate.Text);
                
              cmd.CommandText = (@"INSERT INTO Registration(enrollNumber,fullName,fatherName,motherName,Religion,Gender,admissionYear,Mobile,email,previousSchool,address,facility,fatherOccupation,Age) 
                                     VALUES (@enrollNumber,@fullName,@fatherName,@motherName,@Religion,@Gender,@admissionYear,@Mobile,@email,@previousSchool,@address,@facility,@fatherOccupation,@Age)", con);
                cmd.Parameters.AddWithValue("@enrollNumber", enrol);
                cmd.Parameters.AddWithValue("@@fullName", fullname);
                cmd.Parameters.AddWithValue("@fatherName", fathername);
                cmd.Parameters.AddWithValue("@motherName", mname);
                cmd.Parameters.AddWithValue("@Religion", rel);
                cmd.Parameters.AddWithValue("@Gender", dropGen);
                cmd.Parameters.AddWithValue("@admissionYear", adm);
                cmd.Parameters.AddWithValue("@Mobile", mobile);
                cmd.Parameters.AddWithValue("@email", eml);
                cmd.Parameters.AddWithValue("@previousSchool", prvschl);
                cmd.Parameters.AddWithValue("@address", addr);
                cmd.Parameters.AddWithValue("@facility", facility);
                cmd.Parameters.AddWithValue("@fatherOccupation", occ);
                cmd.Parameters.AddWithValue("@Age",ag);
                con.Open();
                   cmd.ExecuteNonQuery();
                Label1.Text = "Record Added";
                con.Close();
           
        }
Posted
Updated 17-Jun-15 19:41pm
v2

1 solution

don't share the commands and connections, you better create and dispose connection as below

C#
using (OleDbConnection conn = new OleDbConnection(connString))
    {
        string query= "INSERT INTO Registration(enrollNumber,fullName) VALUES (@enrollNumber,@fullName)";            
        using (OleDbCommand myCommand = new OleDbCommand(query, conn))
        {

            myCommand.Parameters.AddWithValue("@enrollNumber", enrol);
            myCommand.Parameters.AddWithValue("@fullName", fullname);
            conn.Open();
            myCommand.ExecuteNonQuery();
            
        }

    }


add all the parameters, in above sample code I have added two parameters only
 
Share this answer
 
Comments
MayankSemwal 18-Jun-15 2:24am    
@DamithSL, did as you suggested error is still the same..
DamithSL 18-Jun-15 2:37am    
what is the full error?
MayankSemwal 18-Jun-15 2:52am    
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

Source Error:


Line 76: myCommand.Parameters.AddWithValue("@Age", ag);
Line 77: conn.Open();
Line 78: myCommand.ExecuteNonQuery();
Line 79: conn.Close();
Line 80: }
DamithSL 18-Jun-15 3:37am    
what is the column data type for AGE column in your database? if it is number you better convert it as int value
int ag = int.Parse(dateTextbox.Text);
MayankSemwal 18-Jun-15 4:47am    
it was number before.. so i made all columns "text" but its still not working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900