andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi,
I got an error stating, " Input string was not in a correct format" when trying to insert a window form data in to a SQL database table.
I got a combobox named "cmbCostCentre" and when the user selects a cost centre, the respective Cost Centre ID is retrieved to another textbox named, "txtCostCentreID" on the same form.
Please refer the below code for the respective "Cost Centre ID" retrieval.
My effort is, when the data of the form is saving to the SQL database table, I want to pass both the "Cost Centre" data and the "Cost Centre ID" Data.
I am using the below command to accomplish the task;
Note
The datatype of the Cost Centre ID is "int" and the Cost Centre is "varchar".
Appreciate your valuable feedback.
Kind regards,
Andrew
I got an error stating, " Input string was not in a correct format" when trying to insert a window form data in to a SQL database table.
I got a combobox named "cmbCostCentre" and when the user selects a cost centre, the respective Cost Centre ID is retrieved to another textbox named, "txtCostCentreID" on the same form.
Please refer the below code for the respective "Cost Centre ID" retrieval.
C#:
private void cmbCostCentre_SelectedIndexChanged(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT costCentre, costCentreCode FROM tbl_CostCentre WHERE costCentre = @acostCentre ", con);
cmd.Parameters.AddWithValue("@acostCentre", cmbCostCentre.Text);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
string costCentreCode = (string)dr["costCentreCode"].ToString();
txtCostCentreID.Text = costCentreCode;
}
con.Close();
}
My effort is, when the data of the form is saving to the SQL database table, I want to pass both the "Cost Centre" data and the "Cost Centre ID" Data.
I am using the below command to accomplish the task;
C#:
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_UserData (costCentreID,costCentre) VALUES (@acostCentreID,@acostCentre)", con);
cmd.Parameters.Add("@acostCentreID", SqlDbType.Int);
cmd.Parameters["@acostCentreID"].Value = int.Parse(txtCostCentreID.Text);
cmd.Parameters.Add("@acostCentre", SqlDbType.VarChar, 150);
cmd.Parameters["@acostCentre"].Value = cmbCostCentre.Text;
Note
The datatype of the Cost Centre ID is "int" and the Cost Centre is "varchar".
Appreciate your valuable feedback.
Kind regards,
Andrew
Last edited by a moderator: