Sunday, February 19, 2012

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 helpHi,
i suppose the SQL should look like this.

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

Hope this helps.

Regards...|||thanks but it still doesnt work|||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...|||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 ?

No comments:

Post a Comment