insert querry doubt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iprogrammer
    New Member
    • Feb 2008
    • 14

    insert querry doubt

    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=.\SQLEXP RESS;AttachDbFi lename=C:\Inetp ub\wwwroot\WebS ite1\App_Data\t est.mdf;Integra ted Security=True;U ser Instance=True") ;
    conn.Open();
    SqlCommand command = new SqlCommand("ins ert into test1 values(mc,pt)", conn);
    command.Execute NonQuery();
    conn.Close();

    }

    b"cos this syntax gives a error:

    System.Data.Sql Client.SqlExcep tion: The name "mc&quo t; 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.Sql Client.SqlConne ction.OnError(S qlException exception, Boolean breakConnection )
    at System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion exception, Boolean breakConnection )
    at System.Data.Sql Client.TdsParse r.ThrowExceptio nAndWarning(Tds ParserStateObje ct stateObj)
    at System.Data.Sql Client.TdsParse r.Run(RunBehavi or runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleR esultSet bulkCopyHandler , TdsParserStateO bject stateObj)
    at System.Data.Sql Client.SqlComma nd.RunExecuteNo nQueryTds(Strin g methodName, Boolean async)
    at System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe)
    at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery()
    at FirstService.re gister(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...
  • ramdil
    New Member
    • Dec 2007
    • 46

    #2
    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

    Comment

    Working...