Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert null value in database if that variable is blank, but in command string it insert blank instead of null. so give error "
Incorrect syntax near ','

". i am posting my code with Command string and error message in following. thanks in advance.

What I have tried:

for (int i = 0; i < datProjectCostDetail.Rows.Count; i++)
{
int? PageSizeID;
double? RatePerPage;
int? TotalPage;
if (datProjectCostDetail.Rows[i][6].ToString() == "")
{PageSizeID = null; }
else
{PageSizeID = (int) datProjectCostDetail.Rows[i][6]; }

if (datProjectCostDetail.Rows[i][9].ToString() == "")
{ RatePerPage = null; }
else
{ RatePerPage = (double) datProjectCostDetail.Rows[i][9]; }

if (datProjectCostDetail.Rows[i][10].ToString() == "")
{ TotalPage = null; }
else
{ TotalPage = (int) datProjectCostDetail.Rows[i][10]; }

cmd = new SqlCommand("Insert into TblProjectCostDetail values(" + ProjectID + ", " + datProjectCostDetail.Rows[i][0] + ", '" + datProjectCostDetail.Rows[i][2].ToString() + "', " + datProjectCostDetail.Rows[i][3] + ", '" + datProjectCostDetail.Rows[i][5].ToString() + "', " + PageSizeID + ", '" + datProjectCostDetail.Rows[i][8].ToString() + "', " + RatePerPage + ", " + TotalPage + ", '" + datProjectCostDetail.Rows[i][11].ToString() + "', " + datProjectCostDetail.Rows[i][12] + " )", con, trans);
               cmd.ExecuteNonQuery();




OutPut of Command Query
Insert into TblProjectCostDetail values(1, 1, '09/Sep/2016', 1, 'Miscellaneous Payment', , '', , , 'Discount', 600 )
Posted
Updated 21-Sep-16 0:58am

Dont concatenate the sql statements, it will lead to sql injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
by doing so, it will be simplify your sql statement and dont need to bother on the datatype formats

try like this
cmd = new SqlCommand("Insert into TblProjectCostDetail values(@col1,@col2,@col3,...colN)", con, trans);
         cmd.Parameters.Add("@col1", ProjectID);
         cmd.Parameters.Add("@col2", datProjectCostDetail.Rows[i][0]);
         cmd.Parameters.Add("@col3",datProjectCostDetail.Rows[i][2]);
         .
         .
         .
         cmd.Parameters.Add("@colN", datProjectCostDetail.Rows[i][12].ToString());
         cmd.ExecuteNonQuery();



use this is insert null values when it is String.Empty
string value = string.IsNullOrWhiteSpace(datProjectCostDetail.Rows[i][0].ToString()) ? null : datProjectCostDetail.Rows[i][0].ToString();
 
Share this answer
 
v2
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that will both protect your DB, and cure your problem.
 
Share this answer
 
Pass DBNull.Value to database if string is empty. You can simply check that by string.IsNullOrWhiteSpace
 
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