Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 749
Reinhardt Avatar asked Oct 04 '22 05:10

Reinhardt


1 Answers

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

like image 130
Rohit Avatar answered Oct 07 '22 22:10

Rohit