Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL database query
Please correct the code:
 
cmdupdate.CommandText = "Insert Into RawMat " +
                " (ProductDimension) " +
                " VALUES ('" + txtwidth.Text + "', '" + txtheight.Text + "', '" + txtunitofmeasurement.Text + "')";
            cmdupdate.ExecuteNonQuery();
 

 
Output:
 
Given:
height = 5
width = 7
unit = feet
 
Answer:
dimension = 5 x 7 feet
Posted 2-Jul-12 21:12pm
wolfsor564
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

cmdupdate.CommandText = "Insert Into RawMat " +
                " (ProductDimension) " +
                " VALUES ('" + txtwidth.Text + " X " + txtheight.Text + " " + txtunitofmeasurement.Text + "')";
            cmdupdate.ExecuteNonQuery();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1) you can concatenate in tsql with + sing
 
2) why don't you concatenate in C#?
3) use parameters for statement building (avoid exposing your app to SQLinjection)
 
cmdupdate.CommandText = "Insert Into RawMat (ProductDimension) VALUES (@dim)";
cmdupdate.Parameters.AddWithValue("@dim", string.Format("{0} x {1} {2}", txtwidth.Text, txtheight.Text, txtunitofmeasurement.Text));
  Permalink  
v2
Comments
wolfsor at 3-Jul-12 3:48am
   
how do you add WHERE ProductNo=@Pno
Zoltán Zörgő at 3-Jul-12 4:05am
   
WHERE clause in this insert statement? I don't understand. In a select you can do it just like here. If data type of the parameter is not detectable, you can use Add method and then set the value. Consult MSDN.
Zoltán Zörgő at 4-Jul-12 17:14pm
   
any progress?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,157
2 Maciej Los 3,774
3 Peter Leow 3,448
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100