Monday, March 12, 2012

INSERT statement - autonumbering

Hi All,

I am having trouble with a simplet INSERT statement. I want to insert a record. The field "dbid" is the primary key and should be autonumbering. What do I need to add to my code?

Dim myconnectionAs SqlConnection myconnection =New SqlConnection() myconnection.ConnectionString = _ ConfigurationManager.ConnectionStrings("infoNoticeDBConnectionString").ConnectionStringDim strSQLAs String ="INSERT INTO users " & _"(dbid, infoid) VALUES (@.dbid, @.infoid)"Dim dbCommAs New SqlCommand(strSQL, myconnection) dbComm.Parameters.Add("infoid", SqlDbType.NVarChar, 50,"@.infoid") dbComm.Parameters.Add("dbid", SqlDbType.UniqueIdentifier,"dbid") dbComm.Parameters("infoid").Value = gv2.SelectedValueTry myconnection.Open() dbComm.ExecuteNonQuery()Catch exAs Exception Response.Write(ex.Message) Response.End()Finally If myconnection.State = ConnectionState.OpenThen myconnection.Close()End If End Try Response.Write("A new record has been added") Response.End()End Sub

Thanks,


If that field has been configured in your database as an autonumber column, then you wouldnot include it in the insert statement.
The database will automatically populate it with the next value.

|||

Got it..Thanks. also had to make field Indentity "yes"

Thanks!

No comments:

Post a Comment