blah blah blah is here! blah blah » Close

up0down
link

Hi All,

I am trying to insert data in one of the tables using SQL Server. Below is the sql command:

string insertSQL = "Insert into CUSTOMERS (" +
" CUSTOMER_NAME" +
") VALUES (" +
custname.Text +
")";

SqlCommand insertcmd = new SqlCommand(insertSQL, myConnection);

//Add parameters
insertcmd.Parameters.AddWithValue("@ID", custname.Text);

When I insert the customer name in the custname text box it is throwing me an error called "Invalid column name 'ABC'". While ABC is the customer name entered in the custname text box.

Please suggest!!

Regards,

last answered one year ago

1 answers

up0down
link

maybe you are trying to do:

string insertSQL = "Insert into CUSTOMERS (CUSTOMER_NAME) VALUES ('" +custname.Text +"')"; 

//where CUSTOMER_NAME is a column name?

string insertSQL = "Insert into CUSTOMERS ([CUSTOMER_NAME]) VALUES ('" +custname.Text +"')";

yes, customer_name is a column name.

Hi. Thank you so much! I was just missing an apostrophe. Got it worked. Another question is: When the customer name is stored in the database it creates a unique id against this name in the same table under another primary column called "ID. Now I have another master table where I have created foreign key with this ID I want to refer this customer name to the serial number in the master table. How do I set the relationship?

take a look here: http://www.sql-tutorial.net/SQL-JOIN.asp (i use joins) can't help you with relationships ;)

Ex. string strSQLCommand = "select id, scr from ndus JOIN nderm ON ndus.id=nderm.id where (nderm.codeus LIKE '" + us.Text + "')";

Thanks! I will try.

Feedback