Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string cmd = string.Format("Update mobilesubscriberdata1 set " + fieldname + "='" + val + "' where m_date='{0}''{1}'", dateTimePicker1.Value.ToString("yyyy-MM-dd")   + " and id=" + Id + "");

obj.GetDataTable(cmd);

can any one tell me what is the error in above command...........
pls
Posted
Updated 14-Jul-14 22:11pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Jul-14 4:12am    
This is not a valid or sensible question. What is the question? "Can anyone change..?" or "Can anyone tell..?". Is that what you wanted to know? :-) Yes, anyone can change (but why?), and no, no one can tell you unless you tell us what did you try to achieve...
—SA

The major problem of your code is the whole idea of composing the query by concatenating some strings to some strings taken from UI. Bad idea. First, repeated string concatenation is really bad, because strings are immutable (do I have to explain this?); you should rather use string.Format.

But, much worse, this code opens wide doors to a well-known exploit called SQL Injection. Here is how:
http://xkcd.com/327[^].

For further explanation, please see my past answers and referenced pages:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

This is what you should use: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

—SA
 
Share this answer
 
The error is that you are using string concatenation to create SQL commands, which can lead to corruption or even total destruction of your database. Use proper parameterised queries as described in the documentation and protect yourself from hackers.
 
Share this answer
 
C#
string cmd = string.Format("Update mobilesubscriberdata1 set " + fieldname + "='" + val + "' where m_date='{0}'" and id=" + Id + "",dateTimePicker1.Value.ToString("yyyy-MM-dd"));

for cleaner purpose

C#
string cmd = string.Format("Update mobilesubscriberdata1 set {0}='{1}' where m_date='{2}' and id={3}",fieldname,val,dateTimePicker1.Value.ToString("yyyy-MM-dd"),Id);
 
Share this answer
 
v3
Comments
Member 10891595 15-Jul-14 4:37am    
thnks sir
ashok rathod 15-Jul-14 6:25am    
please mark it as answer or rate it.
Try:
C#
string cmd = string.Format("Update mobilesubscriberdata1 set {0}='{1}' where m_date='{2}' and id={3}", fieldname.ToString(), val.ToString(), dateTimePicker1.Value.ToString("yyyy-MM-dd"), Id.ToString());
 
Share this answer
 
I guess you get a runtime error .
Am I right?

First of all, this is a terrible mix of string concatenations.
Very difficult to read, hence difficult to trouble shoot.
C#
string cmd = string.Format("Update mobilesubscriberdata1 set " + fieldname + "='" + val + "' where m_date='{0}''{1}'", dateTimePicker1.Value.ToString("yyyy-MM-dd")   + " and id=" + Id + "");
 
obj.GetDataTable(cmd);


Change the code to this
C#
string cmd = string.Format(@"
UPDATE mobilesubscriberdata1 SET fieldname = '{0}' 
WHERE m_date = '{1}''{2}' AND id = '{3}'", 
val, dateTimePicker1.Value.ToString("yyyy-MM-dd"), id);
 
obj.GetDataTable(cmd);


(Not sure if fieldname is a variable or column name. I assumed a column name)
If you take a look now you might see something suspicious.
Hint: Count the arguments. (Why two for the date but only 1 argument?)
 
Share this answer
 
v2

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