-8

I am trying to implement this

    Console.WriteLine("Enter Name: ");
    this.name = Console.ReadLine();
    string sql1 = "insert into items values ( " + this.name ")";
    DataAccess.ExecuteSQL(sql1);

when I try to input data through this it showing error about , unhanded exception , column name or number not found.

I am sure column name is ok and I gave it varchar(50) type. Is this method not permitted?

Thank you in advance.

eshmam
  • 1
  • 3

2 Answers2

0

You need to provide quotes if the field is a varchar:

string sql1 = "insert into items values ( '" + this.name + "')";

Be careful of sql injection though, parameterized queries are better.

Glen Hughes
  • 4,712
  • 2
  • 20
  • 25
0

Well first of all you don't have quotes around your string. You're also missing a plus sign. It should be like:

string sql1 = "insert into items values ( '" + this.name + "')";

However, this is a really bad way of handling your SQL queries through C#. You should be using parameterized queries! There are a lot of bad things that can happen to your database if you do things like this...

See the example of using the SqlCommand class with parameters at the bottom of this page: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79