×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

convert a string to a decimal before update a table or insert data into table

convert a string to a decimal before update a table or insert data into table

convert a string to a decimal before update a table or insert data into table

(OP)
I'd like to ask you how to convert a string into a decimal before update a table or insert data into table, after a user inserts a row, or update a row from a dataGridView.

In my case, I want the column of price to be decimal, I do it as following, but it is not work, please help, thanks.

Error message
Must declare the scalar variable "@p4".


pInsert[3] = new SqlParameter("Price", SqlDbType.Decimal);
pInsert[3].Precision = 18;
pInsert[3].Scale = 3;

pUpdate[2] = new SqlParameter("price", SqlDbType.Decimal);
pUpdate[2].Precision = 18;
pUpdate[2].Scale = 3;


In form1.css:
..............
//after a user click the save button
private void btnSave_Click(object sender, EventArgs e)
{
Infor.UpdateDataSet((DataSet)dataGridView1.DataSource);
}
.........

Infor.css




public static void UpdateDataSet(DataSet ds)
{
SqlConnection cnn = new SqlConnection(strConn);

string sqlInsert, sqlUpdate, sqlDelete;
sqlInsert = "insert into customers(customerid,companyname,
contactname,price) values(@p1,@p2,@p3,@p4)";
sqlUpdate = "update customers set companyname=@p2,
contactname=@p3,price=@p4 where customerid=@p1";
sqlDelete = "delete from customers where customerid=@p1";

SqlParameter[] pInsert = new SqlParameter[4];
SqlParameter[] pUpdate = new SqlParameter[4];
SqlParameter[] pDelete = new SqlParameter[1];

pInsert[0] = new SqlParameter("@p1", SqlDbType.VarChar, 5,
"CustomerID");
pInsert[1] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
"CompanyName");
pInsert[2] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
"ContactName");
pInsert[3] = new SqlParameter("Price", SqlDbType.Decimal);
pInsert[3].Precision = 18;
pInsert[3].Scale = 3;


pUpdate[0] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
"CompanyName");
pUpdate[1] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
"ContactName");
pUpdate[2] = new SqlParameter("price", SqlDbType.Decimal);
pUpdate[2].Precision = 18;
pUpdate[2].Scale = 3;


pUpdate[3] = new SqlParameter("@p1", SqlDbType.VarChar, 5,
"CustomerID");

pDelete[0] = new SqlParameter("@p1", SqlDbType.VarChar, 5,
"CustomerID");

SqlCommand cmdInsert = new SqlCommand(sqlInsert,cnn);
SqlCommand cmdUpdate = new SqlCommand(sqlUpdate,cnn);
SqlCommand cmdDelete = new SqlCommand(sqlDelete,cnn);

cmdInsert.Parameters.AddRange(pInsert);
cmdUpdate.Parameters.AddRange(pUpdate);
cmdDelete.Parameters.AddRange(pDelete);

SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = cmdInsert;
da.UpdateCommand = cmdUpdate;
da.DeleteCommand = cmdDelete;
da.Update(ds, "customers");
ds.AcceptChanges();
}

RE: convert a string to a decimal before update a table or insert data into table

(OP)
I got the solution already.


pInsert[3] = new SqlParameter("@p4", SqlDbType.Decimal, 18, "price");
pInsert[3].Precision = 18;
pInsert[3].Scale = 3;
pInsert[3].Value = "";

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close