IGottaGo IGottaGo - 9 months ago 57
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 = "............"
Query = "INSERT INTO Transacoes(tpAccount, dateTransaction, descrTransaction, amoutTransaction, balanceTransaction)
VALUES(@tpAccount, @dateTransaction, @ddescrTransaction, @amoutTransaction, @balanceTransaction)"

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


Catch ex As Exception
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


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.