I am trying to store data in the sql database using vb.net. In my code I am using parameters to add values.. Below is my code:
Dim lclAmount = txtQty.Text * txtUnitPrice.Text
con = New SqlConnection(connectionString)
' con.Open()
query = "INSERT INTO TBLExpensesList (ExpensesType, Purpose, Qty, UnitPrice, Amount, DoP, Description) VALUES (@val1, @val2, @val3, @val4, @val5, @val6, @val7)"
cmd = New SqlCommand(query, con)
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val1"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val2"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val3"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val4"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val5"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val6"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val7"
cmd.Parameters("@val1").Value = ComboBox1.SelectedItem
cmd.Parameters("@val2").Value = txtPurpose.Text
cmd.Parameters("@val3").Value = txtQty.Text
cmd.Parameters("@val4").Value = txtUnitPrice.Text
cmd.Parameters("@val5").Value = lclAmount
cmd.Parameters("@val6").Value = dtpDOP.Value
cmd.Parameters("@val7").Value = txtDesc.Text
If con.State = ConnectionState.Closed Then
con.Open()
End If
' cmd.CommandText = query
' cmd.Connection = con
'Create Data Adaptor
' sqladp.SelectCommand = cmd
'Create & Fill Data Set
'sqladp.Fill(ds, "TBLExpensesList")
'Get Data Table
'dtbl = ds.Tables("TBLExpensesList")
cmd.ExecuteNonQuery()
If con.State = ConnectionState.Open Then
con.Close()
End If
MessageBox.Show("Data Inserted Successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.None)
'Execute Command
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
I am successfully able to login into my project when the useid and passwords matches the stored values in the database. But when i try to store values I got Inserted successfully and after that When I try to read the rows I got one row. When I close my project ad try to see the values it the database I got no value in the database.. I am also confusing that why the values are not being stored in the database????