Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello team,

I am creating one Windows application that is going to be use by India & Netherland users.
Application is working fine in India - users machine.
But when Netherland users trying to Insert Data into database...Program giving me error.

My Qyery is -
INSERT INTO tbl fld1,fld2,fld3 values ("+val1.Text+","+val2.Text+","+val3.Text+")

Value is fld1 = "10,000.00"

Value is fld2 = "20,000.00"

Value is fld3 = "30,000.00"


Here problem is due to comma, comma is treated as field seperater...

Please suggest me approve so that we have handle this problem.


Thanks,
Pravin Karne
Posted

use parameters like below

C#
int val1 = int.Parse(val1.Text, NumberStyles.AllowThousands);
int val2 = int.Parse(val2.Text, NumberStyles.AllowThousands);
int val3 = int.Parse(val3.Text, NumberStyles.AllowThousands);
cmd.CommandText = "INSERT INTO tbl (fld1,fld2,fld3) VALUES (@val1, @val2, @val3)";
cmd.Parameters.AddWithValue("@val1", val1);
cmd.Parameters.AddWithValue("@val1", val2);
cmd.Parameters.AddWithValue("@val1", val3);


if you have issue with int.parse method for inputs, you can try with int.TryParse

C#
int num;
if (int.TryParse(val1.Text, NumberStyles.AllowThousands,
                 CultureInfo.InvariantCulture, out num))
{
    // parse success
}
 
Share this answer
 
v2
Comments
Pravinkarne.31 5-May-14 8:35am    
Good idea but when I required to insert around 35-40 fields, Then this will be too-long cut
have you used parameters for SQLCommand class?
if not check by inserting value through parameters
 
Share this answer
 
Comments
Pravinkarne.31 5-May-14 8:35am    
Good idea but when I required to insert around 35-40 fields, Then this will be too-long cut
Brahmmam 6-May-14 1:11am    
That is not a problem if you have 35-40 fields and Security will be high if you use parameters in your query.
The proper way to do it is to use parameters. Right now, your database is open to SQL injection attacks.

Do something like this:

SQL
cmd.CommandText = "INSERT INTO tbl (fld1,fld2,fld3) VALUES (@val1, @val2, @val3)";
cmd.Parameters.AddWithValue("@val1", val1.Text);
...
 
Share this answer
 
Comments
Pravinkarne.31 5-May-14 8:34am    
Good idea but when I required to insert around 35-40 fields, Then this will be too-long cut
ZurdoDev 5-May-14 8:38am    
Regardless of how many you have, you never want to jeopardize security just because it might save you a few lines of code. I have stored procedures that take 30+ parameters so there is nothing unreasonable about doing it the right way.

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