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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem structuring an update SQL statement for a FLOAT type in C# 1

Status
Not open for further replies.

iRead

Programmer
Mar 12, 2004
25
US
Any help will be greatly appreciated!

I have a problem in my edit control on a datagrid. All of the fields are varchar except amount which is float. The problem is when I try to build my SQL update string. With this line in I get an error. With this line out no error:

"amount = '" + famount.Replace("'","''")+ "'," +

What am I doing wrong in the following code?

Thanks iRead



public void People_Update(Object sender, DataGridCommandEventArgs e)
{
string FirstName = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string MiddleName = ((TextBox)e.Item.Cells[2].Controls[1]).Text;
string LastName = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
string socialSecurityNumber = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
string amount = ((TextBox)e.Item.Cells[5].Controls[1]).Text;

float famount = (float)Convert.ToDouble(amount);

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["DataSource"]);
connUpdate.Open();
String sql_edit = "UPDATE ContactMatch" +
" SET FirstName = '" + FirstName.Replace("'","''")+ "'," +
"MiddleName = '" + MiddleName.Replace("'","''")+ "'," +
"LastName = '" + LastName.Replace("'","''")+ "'," +
"amount = '" + famount.Replace("'","''")+ "'," +
"socialSecurityNumber = '" + socialSecurityNumber.Replace("'","''")+ "'" +
" WHERE contactID = " + e.Item.Cells[0].Text;

SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
sqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close();

sql = "Select * FROM ContactMatch";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

 
>float famount = (float)Convert.ToDouble(amount);
Here the famount is a float type;
>"amount = '" + famount.Replace("'","''")+ "'," +
Replace() it is not a member of a float type and this is the error.
The amount should not be enclosed in quotes, so modify the string to look like :
string sql_edit = "UPDATE ContactMatch SET FirstName = 'George',LastName = 'Smith', amount = 34000.56 where contactID= 300";

So, just use amount instead of famount.

-obislavu-



 
obislavu

I thought I had tried exactly that earlier but apparently not. I made the change as you suggested and it worked brilliantly.

Thank you very much!
iRead
 
Note that if you had used ADO.NET parameter objects, this would not have been an issue.

There is a FAQ in the VB Databases forum (VB6, sorry, I keep meaning to update it for C# but never have the time), where it explains why using them is such a good idea.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top