By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

insert querry doubt

P: 14
i m new to .net and db connectivity in .net using sql server....

i want to insert values from into a db test1 from a function register(); where i will pass values the function register
will this syntax work...

public void register(int mc,int pt)
{

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Inetpub\ww wroot\WebSite1\App_Data\test.mdf;Integrated Security=True;User Instance=True");
conn.Open();
SqlCommand command = new SqlCommand("insert into test1 values(mc,pt)", conn);
command.ExecuteNonQuery();
conn.Close();

}

b"cos this syntax gives a error:

System.Data.SqlClient.SqlException: The name "mc" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at FirstService.register(Int32 mc, Int32 pt)

is this error...b"cos of sql insert in an ways...if it is wht shld be the correct way to fire the querry...
Mar 21 '08 #1
Share this Question
Share on Google+
1 Reply


P: 46
Hi

Checking your insert statments, i think parameter are also included as hardcoded values.Check the insert statments creation in your code.
for eg
"INSERT INTO simplesql (simple_id, simple_text) VALUES (" + param1 + ",'" + param2+ "')"

your insert statements also should be like this

Hope this helps you
Mar 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.