Q:

How to input a value on a web form and store it as numeric in SQL Server

I have the following code:
protected void btn_add_Click(object sender, EventArgs e)
{
    if (intSlipID_srch.Text == "")
        lblStatus.Text = "Please enter a Slip ID.  ";
    string connection =
    WebConfigurationManager.ConnectionStrings["popeye_marina"].ConnectionString;
    SqlConnection con = new SqlConnection(connection);

    // Clear text controls

    txtSlipLength.Text = string.Empty;
    txtSlipWidth.Text = string.Empty;
    txtCovered.Text = string.Empty;
    txtFee.Text = string.Empty;
    intDockID.Text = string.Empty;
    intBoatID.Text = string.Empty;

    // Create Update SQL string

    string insertSQL;
    insertSQL = "INSERT INTO slip (";
    insertSQL += "slip_length, slip_width, ";
    insertSQL += "covered, annual_fee, dock_id, boat_id) ";
    insertSQL += "VALUES (";
    insertSQL += "@slip_length, @slip_width, @covered, ";
    insertSQL += "@fee, @dockID, @boatID)";

    SqlCommand cmd = new SqlCommand(insertSQL, con);
    cmd.Parameters.AddWithValue("@slip_length", txtSlipLength.Text);

    cmd.Parameters.AddWithValue("@slip_width", txtSlipWidth.Text);
    cmd.Parameters.AddWithValue("@covered", txtCovered.Text);
    cmd.Parameters.AddWithValue("@fee", txtFee.Text);
    cmd.Parameters.AddWithValue("@dockID", intDockID.Text);
    cmd.Parameters.AddWithValue("@boatID", intBoatID.Text);
    int inserted = 0;
    try
    {
        con.Open();
        inserted = cmd.ExecuteNonQuery();
        lblStatus.Text = inserted.ToString() + " record inserted.";
    }
    catch (Exception err)
    {
        lblStatus.Text = "Error saving Slip.  ";
        lblStatus.Text += err.Message;
    }
    finally
    {
        con.Close();
    }
}

The target database column is numeric. When I run the web form and enter a numeric value, I get the error:

Error converting data type nvarchar to numeric

Can you tell me how to correct this error?

A:

there's a two mistakes here,
first, why you clear the textbox before assign it to a new variable. it would always retrun a null string.
txtSlipLength.Text = string.Empty;
txtSlipWidth.Text = string.Empty;
txtCovered.Text = string.Empty;
//...

second is, you need to convert your value to an appropriate data type with your table.
ex:
//if your column is Decimal
cmd.Parameters.AddWithValue("@slip_length", Convert.ToDecimal(txtSlipLength.Text));
//if your column is Integer
cmd.Parameters.AddWithValue("@slip_length", Convert.ToInt32(txtSlipLength.Text));
//if your column is string (nvarchar, varchar, char, etc.) then you can use your code
cmd.Parameters.AddWithValue("@slip_length", txtSlipLength.Text);

