Inserting Values to Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rekhasc
    New Member
    • Oct 2007
    • 66

    Inserting Values to Database

    Front end: Asp.net
    Code: C#
    Backend:Msacces s

    i am doing project on loan, i have to insert the customer values to the database, its showing errors in cmd.executenonq uery,


    public partial class _Default : System.Web.UI.P age
    {
    OleDbConnection con;
    OleDbCommand cmd;

    protected void Page_Load(objec t sender, EventArgs e)
    {
    con=new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;user id= Admin ; password=; Data Source=D:\\Webl oanmonitor\\loa n.mdb");

    }
    protected void Button1_Click(o bject sender, EventArgs e)
    {
    int i;
    string str;
    str = "insert into LoanApplication values('" +txtfullname+ "','" +txtfather+ "','" +txtdob+ "','" +txtage+ "','" +RadioButtonLis t1+ "','" +txteducation+ "','" +ddlistmartial+ "','" + txtmobile + "','" +txtphone+ "','" +txtemail+ "','" +txtresidential + "','" +txtpermanent+ "')";
    cmd = new OleDbCommand(st r,con);
    try
    {
    con.Open();
    i = cmd.ExecuteNonQ uery();
    con.Close();
    if (i > 0)
    {
    Response.Redire ct("EmploymentD etails.aspx");
    }
    }
    catch (Exception ex)
    {
    Response.Write( "error" + ex);
    }

    }
    }

    this is my code , please help me;
    i also tried this
    insert into LoanApplication (fullname,fathe r,dob.......)Va lues('"+txtfull name+'",....... ....)
  • deric
    New Member
    • Dec 2007
    • 92

    #2
    What is the error message?
    Try to transfer this line: cmd = new OleDbCommand(st r,con);
    after the line con.Open();

    By the way, use CODE tags around your code. Please read the guidelines on the right.. Thanks.

    Comment

    • rekhasc
      New Member
      • Oct 2007
      • 66

      #3
      hi...
      the error is data type mismatch



      Please Enter ProperlySystem. Data.OleDb.OleD bException: Data type mismatch in criteria expression. at System.Data.Ole Db.OleDbCommand .ExecuteCommand TextErrorHandli ng(OleDbHResult hr) at System.Data.Ole Db.OleDbCommand .ExecuteCommand TextForSingleRe sult(tagDBPARAM S dbParams, Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteCommand Text(Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteCommand (CommandBehavio r behavior, Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteReaderI nternal(Command Behavior behavior, String method) at System.Data.Ole Db.OleDbCommand .ExecuteNonQuer y() at _Default.Button 1_Click(Object sender, EventArgs e) in d:\Webloanmonit or\Default.aspx .cs:line 32

      here i am attaching my proj, this code is in default.aspx.cs
      Attached Files

      Comment

      • rekhasc
        New Member
        • Oct 2007
        • 66

        #4
        hi..
        thanx for your reply...
        i corrected that error but its showing new errors

        ERROR: OVERFLOW at

        Please Enter Properly::Syste m.Data.OleDb.Ol eDbException: Overflow at System.Data.Ole Db.OleDbCommand .ExecuteCommand TextErrorHandli ng(OleDbHResult hr) at System.Data.Ole Db.OleDbCommand .ExecuteCommand TextForSingleRe sult(tagDBPARAM S dbParams, Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteCommand Text(Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteCommand (CommandBehavio r behavior, Object& executeResult) at System.Data.Ole Db.OleDbCommand .ExecuteReaderI nternal(Command Behavior behavior, String method) at System.Data.Ole Db.OleDbCommand .ExecuteNonQuer y() at _Default.Button 1_Click(Object sender, EventArgs e) in d:\Webloanmonit or\Default.aspx .cs:line 31
        Attached Files

        Comment

        • deric
          New Member
          • Dec 2007
          • 92

          #5
          It seems that some of the values that you provided in your insert statement exceed the allowable value or range of the datatype of your table's column.

          Try to do the following:
          1. Step trace your code if the values in the insert statement is correct. You can Response.Write( str); to display the final value of str, which is holding your insert statement.

          2. Place the name of the columns on your insert.
          Like, "Insert Into Table (column1) Values (value1)".
          With this, there will be consistency in your code and it will be easier to debug or maintain.

          3. For number values, try to not enclose it with single quotes.
          Like, instead of "Insert Into Table (numberColumn) Values ('100')", use "Insert Into Table (numberColumn) Values (100)"


          Try that and let me know what happens.

          Comment

          Working...