Click here to Skip to main content
12,558,712 members (61,358 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ADO.NET C#4.0 Access
Hii everyone,

i'm beginner. After many tries error is still exist (error : input string is incorrect format !! How to solve this error??)

this is my code :
int x;
        string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\org.accdb";
        OleDbConnection con = new OleDbConnection(conString);
        DataTable dt = new DataTable();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        oda.UpdateCommand = new OleDbCommand("update student set id_civil_st=@id_civil_st,names=@names,phone=@phone,address=@address,school_ year=@school_ year,amount_ paid=@amount_ paid,amount_rest=@amount_rest,date_record=@date_record,date_start=@date_start,date_end=@date_end where sn_s=@sn_s", con);
        //in ms access text
        oda.UpdateCommand.Parameters.Add("@id_civil_st", OleDbType.Char).Value = id_civil_st.Text;
        //in ms access datatype : text
        oda.UpdateCommand.Parameters.Add("@names", OleDbType.Char).Value = names.Text;
        //in ms access datatype: text
        oda.UpdateCommand.Parameters.Add("@phone", OleDbType.Char).Value = phone.Text;
        //in ms access datatype: text
        oda.UpdateCommand.Parameters.Add("@address", OleDbType.Char).Value = address.Text;
        //in ms access datatype: text
        oda.UpdateCommand.Parameters.Add("@school_ year", OleDbType.Char).Value = textBox2.Text;
        //in ms access datatype: currency
        oda.UpdateCommand.Parameters.Add("@amount_ paid", OleDbType.Currency).Value =Convert.ToDecimal( textBox3.Text);
        //in ms access datatype currency
        oda.UpdateCommand.Parameters.Add("@amount_rest", OleDbType.Currency).Value =Convert.ToDecimal( textBox4.Text);
        //in ms access dat/time
        oda.UpdateCommand.Parameters.Add("@date_record", OleDbType.Date).Value =Convert.ToDateTime( textBox5.Text);
        //in ms access date/time
        oda.UpdateCommand.Parameters.Add("@date_start", OleDbType.Date).Value =Convert.ToDateTime( textBox6.Text);
        //in ms access date/time
        oda.UpdateCommand.Parameters.Add("@date_end", OleDbType.Date).Value =Convert.ToDateTime( textBox7.Text);
        //in ms access datatype: autonumber
        oda.UpdateCommand.Parameters.Add("@sn_s", OleDbType.Integer).Value =Convert .ToInt32( sn_s.Text);

        x = oda.UpdateCommand.ExecuteNonQuery();
        if (x >= 1)
            MessageBox.Show("Records has been updated");
Posted 25-Sep-12 0:09am
Updated 29-Sep-12 23:47pm
Anele.Ngqandu 25-Sep-12 7:43am
What part of the code do you getthat error? Please put a break point then youl see wher things go wrng. A value is expecting to get a decimal not a string value somewher
mhassan083 26-Sep-12 0:43am
error gets when run and does not refer to any code
Oshtri Deka 26-Sep-12 3:43am
//in ms access datatype: autonumber
oda.UpdateCommand.Parameters.Add("@sn_s", OleDbType.Integer).Value = textboxFirstname.Text;

The last part looks suspicious - textboxFirstname for identity value?!
Or you tend to give strange names to your variables.
mhassan083 26-Sep-12 6:29am
textboxFirstname is the name of text only ,just name
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Are you sure that the textboxes contain the data you expect them to contain?
For example, oda.UpdateCommand.Parameters.Add("@sn_s", OleDbType.Integer).Value = textboxFirstname.Text; looks very strange - is First Name an Integer?
Furthermore, do some parsing for the numerical types from the strings. If you need an integer, use e.g. int.Parse(textbox10.Text); for a decimal, decimal.Parse(textbox11.Text)
mhassan083 26-Sep-12 1:33am
yes i'm sure ,textboxfirstname.text is display sn_s with type autonumber in access and integer here.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Account.mdb");

OleDbCommand cmd = new OleDbCommand("update payment set Type_Name='" + textBox2.Text + "',Amount='" + textBox3.Text + "',Paid_Date='" + textBox4.Text + "',Paid_By='" + textBox5.Text + "',Received_By='" + textBox6.Text + "' where id=" + pid + "", con);
MessageBox.Show("Information updated successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

use this code for update ur value..

mhassan083 26-Sep-12 2:54am
what (pid) do you mean ??textbox or what?!
shambhoo kumar 26-Sep-12 3:12am
same as sn_s
mhassan083 26-Sep-12 3:27am
what is the textbox display it if id=pid pid??!!like Received_BY= textBox6.Text
also after run give me error (error in update syntax)
shambhoo kumar 27-Sep-12 2:05am
u right this code like Received_BY= '"+textBox6.Text+"'
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Hi try with proper conversion as below

oda.UpdateCommand.Parameters.Add("@amount_ paid", OleDbType.Currency).Value = Convert.toDecimal(textBox3.Text); // for currency

oda.UpdateCommand.Parameters.Add("@date_record", OleDbType.Date).Value =Convert.ToDataTime(textBox5.Text); //Date

oda.UpdateCommand.Parameters.Add("@sn_s", OleDbType.Integer).Value = Convert.ToInt( textboxFirstname.Text); //for integer
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161026.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100