-1

I am trying to insert values of checkboxlist into transaction table but it's showing me error that

InCorrect Syntax Near '='.

if (cblAmenity.SelectedIndex >= 0)
{
    for (int i = 0; i < cblAmenity.Items.Count; i++)
    {
        if (cblAmenity.Items[i].Selected)
        {
            ds = obj.sel("Select MAX(PropertyId) AS PrptId from tblPropertyMaster");
            string a = ds.Tables[0].Rows[0]["PrptId"].ToString();
            string nature = "Sell";
            obj.insert("insert into tblAmenityToPropertyTransaction Values (AmenityName=" + i + " , PropertyId=" + a + " , Nature='" + nature + "')");
        }
    }
}

Any help appreciated.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Chiraag
  • 35
  • 10

3 Answers3

4

INSERT INTO in sql server doesn't work in that way

 INSERT INTO table VALUES(value1, value2, value3)

not

 INSERT INTO table VALUES(field=value, field1=value1....)

However, apart from this, try to use always a parameterized query.
In your wrong syntax you use a string concatenation to build your command, but this is an open door to Sql Injection and infinte source of problems when the values concatenated in your string contains invalid characters (for example a single quote inside a string)

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Ohh sosorry sir, even though i've done this so many time as per your syntax i just forget syntax of insert statement because of frustration :( – Chiraag Apr 12 '14 at 20:31
  • The curious thing is that a syntax like that is supported by MySql – Steve Apr 12 '14 at 20:33
0

You are using wrong INSERT statement syntax,

Example Insert statement

insert into table1(col1, col2)
values (val1,val2);

Your statement should be

"insert into tblAmenityToPropertyTransaction (AmenityName,PropertyId, Nature)
Values (" + i + " ," + a + " , '" + nature + "')"

And please use parameterized SQL statements

rs.
  • 26,707
  • 12
  • 68
  • 90
0

You can use the following code:

Insert into tblAmenityToPropertyTransaction (AmenityName,PropertyId, Nature) Values (" + i + " ," + a + " , '" + nature + "')"

Idris
  • 997
  • 2
  • 10
  • 27
Paresh J
  • 2,401
  • 3
  • 24
  • 31