Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

dldl

Programmer
May 3, 2010
40
NZ
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();
}​
 
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 = "";​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top