user2559183
user2559183

Reputation: 35

"Error converting data type nvarchar to numeric."

I want to Insert record to the database with a ComboBox. The ComboBox is connected to the other table and this is the error:

Error converting data type nvarchar to numeric.

private void InsertReceipt()
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)" +
                      "VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) ";
    cmd.Parameters.AddWithValue("@CustomerID", cboName.SelectedValue);
    cmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString());
    cmd.Parameters.AddWithValue("@Store", txtStore.Text);
    cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);
    cmd.Parameters.AddWithValue("@NoStub", txtStub.Text);
    cmd.ExecuteNonQuery();
}

void GetRecords2()
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    cmd.CommandText = "SELECT CustomerID, firstname + ',  ' + lastname AS Name  FROM Customer";

    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds, "Customer");

    cboName.DataSource = ds;
    cboName.DisplayMember = "Customer.Name";
    cboName.ValueMember = "Customer.CustomerID";
}

Upvotes: 0

Views: 11638

Answers (4)

Serkan Köse
Serkan Köse

Reputation: 1

cmd.Parameters.AddWithValue("@ödenecektutar", Convert.ToDecimal(tutar1.Text.Substring(0, tutar.Text.Length - 1)));

Upvotes: -1

OMAR
OMAR

Reputation: 1

 private void button1_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "insert into slab (date,sober_visor,tesh,shift,group,heat_no,st_grade,thick,width,length,location,pcs,remarkes,slab_no) values (@date,@sober_vsor,@tesh,@shift,@group,@heat_no,@st_grade,@thick,@width,@length,@loction,@pcs,@slab_no);select scope_identity()";

            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@date", txt_date.Text);
            cmd.Parameters.AddWithValue("@sober_visor", com_sober_visor.ToString());
            cmd.Parameters.AddWithValue("@shift", txt_shift.Text);
            cmd.Parameters.AddWithValue("@heat_no", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@thick", txt_shift.Text);
            cmd.Parameters.AddWithValue("@width", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@length", txt_length.Text);
            cmd.Parameters.AddWithValue("@pcs", txt_pcs.Text);
            cmd.Parameters.AddWithValue("@st_grade", txt_st_gread.Text);
            cmd.Parameters.AddWithValue("@location", txt_loction.Text);
            cmd.Parameters.AddWithValue("@slab_no", txt_slab_no.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            txt_heat_no.Text = cmd.ExecuteScalar().ToString();

            con.Close();
            MessageBox.Show("تمت عملية الإضافة");
        }
    }
}

Upvotes: -1

Taja_100
Taja_100

Reputation: 449

try string.isNullorEmpty(txtAmount.text);

Upvotes: 0

Michael Bray
Michael Bray

Reputation: 15265

When you call AddWithValue make sure that the type of data you are passing matches the column type. Here's a likely candidate:

cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);

In this line you are passing a text string to something that clearly wants a numeric value (an amount). You should parse the txtAmount.Text into a decimal first and then pass that value:

decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);

With this code, you may still get an exception if the string in txtAmount.Text can't be parsed into a decimal, but at least then you'll know which value is causing the problem. You can/should do the same with the other values as well to make sure they match their column types.

Upvotes: 3

Related Questions