Results 1 to 5 of 5
  1. #1
    Join Date
    May 2003
    Posts
    4

    Question Unanswered: Inserting problem with sqlserver and vb.net

    hi there i ve got a quick question regarding table and adding primary key and values
    Basically i have a empty table called ReligionType and have two fields ReligionID and ReligionName. I would like to add a new data into this table (i set the indentity and identity seed in sql server but when i execute the value in sqladapter it give me error on

    "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll"

    This is the code that i wrote in vb.net


    con2.Open() //sql connection

    Me.SqlDataAdapter2.Fill(DataSet1)

    Me.SqlInsertCommand2.CommandText() = "Insert into ReligionType(ReligionName) Values (@ReligionName) SELECT @@identity As 'ReligionID' GO"

    Me.SqlInsertCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE" , System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))

    Me.SqlInsertCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ReligionName" , System.Data.SqlDbType.NVarChar, 20)).Value = inputdata.Text

    Me.SqlInsertCommand2.ExecuteNonQuery()

    'Me.SqlDataAdapter2.Fill(DataSet1)
    con2.Close()

    so where did i go wrong ?
    please help

  2. #2
    Join Date
    Aug 2004
    Location
    Pune,India
    Posts
    94
    Hi,
    i suppose the SQL should look like this.

    "Insert into ReligionType(ReligionName) Values (@ReligionName); SELECT @@identity As 'ReligionID' GO"

    Hope this helps.

    Regards....

  3. #3
    Join Date
    May 2003
    Posts
    4
    thanks but it still doesnt work

  4. #4
    Join Date
    Apr 2006
    Posts
    33
    The GO command would cause a problem...

    The GO command isn't a T-SQL command (although it works in Query Analyzer). Take it out and give it a try.
    I haven't looked too hard at the rest of the code...
    David Wiseman
    MCSE, MCSA, MCDBA

    www.wisesoft.co.uk

  5. #5
    Join Date
    May 2003
    Posts
    4
    Still when i have remove the word go it still doesnt work
    ps... i forgot to mention that the data in the table is currently empty

    and also have type try and catch statement and this is the result of message

    "Object reference not set to an instance of an object."

    so what did i do wrong ?
    Last edited by sun919; 06-21-06 at 07:07.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •