System.FormatException error!

  • Thread starter Thread starter SSP
  • Start date Start date
S

SSP

Hi all,

I have an application on PPC 2003 which contains a form used to enter some
data into a SQL CE db.

The db contains a table like this:
___________________
cNSA nvarchar (50),
cCN nvarchar (50),
cVN int
--------------------------

Below is a snippet of the code used to enter data into the db where cVN_tb,
cNSA_tb, cCN_tb are textboxes.

when I hit the submit button the I get the error:
________________________________________________________________________
An unhandled exception of type 'System.FormatException' occurred in
System.Windows.Forms.dll
Additional information: FormatException
The thread '<No Name>' (0x61734) has exited with code 0 (0x0).
The thread '<No Name>' (0x24a7e4) has exited with code 0 (0x0).
The program '[2448] rundll32.exe: PRSSForms.exe' has exited with code 0
(0x0).
___________________________________________________________________________

When check the contents of the database, the cVN column (integer column)
manages to get the data, but the nvarchar columns cause the above error.

CODE:
___________________________________________

cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlCeParameter("p2", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlCeParameter("p3", SqlDbType.Int));

cmd.Prepare();

int vFN;
vFN = Convert.ToInt32(cVN_tb.Text);

string cNSA_tb_conv;
cNSA_tb_conv = Convert.ToString(cNSA_tb.Text);

string cCN_tb_conv;
cCN_tb_conv = Convert.ToString(cCN_tb.Text);

cmd.Parameters["p1"].Value = cNSA_tb_conv;
cmd.Parameters["p2"].Value = cCN_tb_conv;
cmd.Parameters["p3"].Value = vFN;
cmd.ExecuteNonQuery();
_______________________________________________

Any ideas anyone??

SSP
 
Why are you calling Convert.ToString() on the Text property of the textbox
for the two varchar fields? - these are already string values.

Peter
 
Convert.ToString was an extra line to ensure the string data is converted to
string. Experimental line of code.

I figured it out though...just had to dig into the references.

Instead of using "SqlDbType", I had to use "DbType" to specify the Parameter
type for the dataprovider. Below is the explanation of "DbType" straight
from the Class Lib. def.

"The type of a parameter is specific to the .NET Framework data provider.
Specifying the type converts the value of the Parameter to the data provider
Type before passing the value to the data source. If the type is not
specified, ADO.NET infers the data provider Type of the Parameter from the
Value property of the Parameter object."

So the new code looks like the following:
CODE:
___________________________________________

cmd.Parameters.Add(new SqlCeParameter("p1", DbType.String));
cmd.Parameters.Add(new SqlCeParameter("p2", DbType.String));
cmd.Parameters.Add(new SqlCeParameter("p3", DbType.Int32));

cmd.Prepare();

int vFN;
vFN = Convert.ToInt32(cVN_tb.Text);

cmd.Parameters["p1"].Value = cNSA_tb.text;
cmd.Parameters["p2"].Value = cCN_tb.text;
cmd.Parameters["p3"].Value = vFN;
cmd.ExecuteNonQuery();
_______________________________________________

SSP

Peter Foot said:
Why are you calling Convert.ToString() on the Text property of the textbox
for the two varchar fields? - these are already string values.

Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

SSP said:
Hi all,

I have an application on PPC 2003 which contains a form used to enter some
data into a SQL CE db.

The db contains a table like this:
___________________
cNSA nvarchar (50),
cCN nvarchar (50),
cVN int
--------------------------

Below is a snippet of the code used to enter data into the db where cVN_tb,
cNSA_tb, cCN_tb are textboxes.

when I hit the submit button the I get the error:
________________________________________________________________________
An unhandled exception of type 'System.FormatException' occurred in
System.Windows.Forms.dll
Additional information: FormatException
The thread '<No Name>' (0x61734) has exited with code 0 (0x0).
The thread '<No Name>' (0x24a7e4) has exited with code 0 (0x0).
The program '[2448] rundll32.exe: PRSSForms.exe' has exited with code 0
(0x0).
___________________________________________________________________________
When check the contents of the database, the cVN column (integer column)
manages to get the data, but the nvarchar columns cause the above error.

CODE:
___________________________________________

cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlCeParameter("p2", SqlDbType.NVarChar));
cmd.Parameters.Add(new SqlCeParameter("p3", SqlDbType.Int));

cmd.Prepare();

int vFN;
vFN = Convert.ToInt32(cVN_tb.Text);

string cNSA_tb_conv;
cNSA_tb_conv = Convert.ToString(cNSA_tb.Text);

string cCN_tb_conv;
cCN_tb_conv = Convert.ToString(cCN_tb.Text);

cmd.Parameters["p1"].Value = cNSA_tb_conv;
cmd.Parameters["p2"].Value = cCN_tb_conv;
cmd.Parameters["p3"].Value = vFN;
cmd.ExecuteNonQuery();
_______________________________________________

Any ideas anyone??

SSP
 
Back
Top