Reinhardt
Reinhardt

Reputation: 143

Failed to convert from a string to a int32

i got a problem where i only be able to retrieve the data from the database on a single textbox. What i want is, i want to retrieve the data of every textboxes from the database, but when i tried, getting error:

Here is the code:

string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");

private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();

private void Form1_Load(object sender, EventArgs e)
{  
    UpdateTextPosition();

    OleDbDataReader dReader;
    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);

    dReader = cmd.ExecuteReader();

    AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();

    while (dReader.Read())
    {
         string numString = dReader[0].ToString().PadLeft(4, '0');
         codesCollection.Add(numString);
    }

    dReader.Close();
    conn.Close();
}

private void UpdateDatas()
{    
    OleDbDataReader dReader;
    OleDbConnection conn = new OleDbConnection(connectionString);
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Description], [Price] FROM [Data] WHERE [Code]=@Code", conn);

    cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
    cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;

    dReader = cmd.ExecuteReader();

    while (dReader.Read())
    {
        this.textBoxDescContainer[0][0].Text = dReader["Description"].ToString();
        this.textBoxSubTotalContainer[0][0].Text = dReader["Price"].ToString();
    }

    dReader.Close();
    conn.Close();
}

I already tried add:

cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][1].Text;

and

while (dReader.Read())
{
    this.textBoxDescContainer[0][1].Text = dReader["Description"].ToString();
    this.textBoxSubTotalContainer[0][1].Text = dReader["Price"].ToString();
}

but i got an error which says: "Failed to convert parameter value from a string to a int32"

Upvotes: 5

Views: 2429

Answers (5)

Rohit
Rohit

Reputation: 10236

Try this

you have to parse the string to Int32 DataType

cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value =int.Parse(this.textBoxCodeContainer[0][1].Text);

This should work if user enters a valid integer

For e.g.

int Val=int.Parse("45");//Works for me

Note: This does not work for decimal values

Alternatively If your Price value contains Decimal what you can do is

cmd.Parameters["Code"].Value = Convert.ToInt32(Convert.ToDouble(this.textBoxCodeContainer[0][1].Text));

This should work

Upvotes: 2

PawanS
PawanS

Reputation: 7193

Change the line

cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;

to

cmd.Parameters["Code"].Value = Convert.ToInt32(this.textBoxCodeContainer[0][0].Text);

Upvotes: 1

RealSollyM
RealSollyM

Reputation: 1530

According to your code, which you don't really mention where the error is raised, this seem to be the source of your problem.

            cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
            cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;

You have declared "Code" as an Integer but you are assigning a Text value. Convert this to Int32.

Int32 codeValue = 0; 
Int32.TryParse(this.textBoxCodeContainer[0][0].Text, out codeValue); 
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
cmd.Parameters["Code"].Value = codeValue;

Upvotes: 0

Ehsan
Ehsan

Reputation: 32671

this line

cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][1].Text;

needs to be changed to

int codeValue;
if(int.TryParse(this.textBoxCodeContainer[0][1].Text,out codeValue))
  cmd.Parameters["Code"].Value = codeValue;
else
  { 
    //do some error handling of invalid input
  }

as you are trying to pass a string to parameter of type ineteger.

Upvotes: 1

Anurag
Anurag

Reputation: 572

this.textBoxSubTotalContainer[0][0].Text=Convert.ToInt32(dReader["Price"].ToString());

The above statement should help you...

Regards

Anurag

Upvotes: 0

Related Questions