Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more: , +
below SqlStatement values must enter other wise not work this sqlStatement. if we are not fill one textbox value, how it work successfully this statement.


C#
try
      {
          con.Open();
          SqlCommand cmd = new SqlCommand("INSERT INTO other_source_income VALUES ('" + txtOSIInterstFromBank.Text + "','"
          + txtOSITDSFromBank.Text + "','"
          + txtOSIInterestFromDebent.Text + "','"
          + txtOSITDSFromNSSWithdr.Text + "','"
          + txtOSIAccuredInterstOnNSC.Text + "','"
          + txtOSITotalIncome.Text + "','"
          + txtOSITotalTDS.Text + "','"
          + txtOSISalary.Text + "','"
          + txtOSIProfessionalTax.Text + "','"
          + txtOSIPFDeduction.Text + "','"
          + txtOSITDS.Text + "')", con);


          cmd.ExecuteNonQuery();
      }


      catch (SqlException ex)
      {
          Response.Write(ex.Message.ToString());
      }
      finally
      {
          con.Close();
      }
Posted
Updated 11-May-12 18:13pm
v2
Comments
Sergey Alexandrovich Kryukov 12-May-12 0:27am    
Perhaps first thing to think about it: why should it work successfully? :-)
--SA

Bad, really bad idea.

One problem of your code is string concatenation. Repeated concatenation is a bad operation, because strings are immutable, so it's a performance problem. Should I explain why? The class System.Text.StringBuilder and the method String.Format are free from this problem.

But much bigger problem is the purpose of your concatenation. This is really a fatal mistake, from the security standpoint. The problem is: you compose a command using the strings taken from the UI, from the user input. But the user can input anything, including some SQL fragments (no, filtering them out is not serious). This opens wide doors to the well-known exploit called SQL injection. Never do it. Please read about this exploit and pay special attention for the importance of parameterized statements:
http://en.wikipedia.org/wiki/SQL_injection[^].

You need to use SQL command parameters. Please read about using command parameters in ADO.NET:
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx[^].

—SA
 
Share this answer
 
Comments
VJ Reddy 12-May-12 23:26pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 13-May-12 0:34am    
Thank you, VJ.
--SA
What I usually do in a situation like this is put the sql statement in a string, then in debug mode can copy the statement and then use SQL-Server management console to execute the statement. Usually gives me a good idea of what the real problem is. Also SA is totally right.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900