IGottaGo IGottaGo - 5 months ago 24
Vb.net Question

Insert value with sql parameters

So, I am developing a simple savings account application where I am able to simulate deposit, withdrawal and transfer of money. I have a button where I can click on a button called "Deposit" and data will be record on my database.

To make that insert statement I am using sql parameters and this is the code:

SqlCon = New SqlConnection
SqlCon.ConnectionString = "............"
Try
Query = "INSERT INTO Transacoes(tpAccount, dateTransaction, descrTransaction, amoutTransaction, balanceTransaction)
VALUES(@tpAccount, @dateTransaction, @ddescrTransaction, @amoutTransaction, @balanceTransaction)"

SqlCon.Open()
SqlCmd = New SqlCommand(Query, SqlCon)

With SqlCmd.Parameters
.Add("@tpAccount", SqlDbType.Char).Value = cbTipoConta.Text
.Add("@dateTransaction", SqlDbType.DateTime).Value = txtDate.Text
.Add("@descrTransaction", SqlDbType.Char).Value = cmdDepositar.Text
.Add("@amoutTransaction", SqlDbType.Int).Value = txtDeposito.Text
.Add("@balanceTransaction", SqlDbType.Int).Value = txtBalance.Text
End With

SqlCmd.ExecuteNonQuery()

SqlCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


But in this table I have a "ID" column but I don't want it to displays on sql parameters and shows me an erro:
Cannot insert the value NULL into column 'Id'
. I should autoincrement the Id and then this will work fine? Or what is the best way to solve this

Answer

You should autoincrement the ID, I always use ID column with Identity(1,1) even though it is not needed at the moment. It is always nice to select specific row fast and when the ID is autoincremented, then you have only one where condition.

Comments