I was trying my best to insert records to a db. I am not getting the records added nor it shows some error. Please help me. I am using localdb with Visual studio 2012.
Imports System.Data.SqlClient
Imports System.Data
Public Class tax_configure
Private Sub tax_configure_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With tax_on_combo
.SelectedIndex = 0
End With
End Sub
Private Sub add_tax_cata_Click(sender As Object, e As EventArgs) Handles add_tax_cata.Click
Dim con As New SqlConnection
Dim com As New SqlCommand
Try
con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\My VB projects\Test App\Test App\bin\Debug\test_db.mdf;Integrated Security=True;Connect Timeout=30"
con.Open()
com.Connection = con
com.CommandText = "INSERT INTO [dbo].[tax_details] ([tax_catagory], [tax_rate], [ed_cess_rate], [s_ed_cess_rate], [tax_on]) VALUES (@tax_catagory, @tax_rate, @ed_cess_rate, @s_ed_cess_rate, @tax_on)"
com.Parameters.Add(New SqlParameter("@tax_catagory", tax_cata_name.Text))
com.Parameters.Add(New SqlParameter("@tax_rate", Val(tax_rate_txt.Text)))
com.Parameters.Add(New SqlParameter("@ed_cess_rate", Val(Ecess_rate_txt.Text)))
com.Parameters.Add(New SqlParameter("@s_ed_cess_rate", Val(SEcess_rate_txt.Text)))
com.Parameters.Add(New SqlParameter("@tax_on", tax_on_combo.SelectedText))
com.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
con.Close()
End Try
End Sub
End Class
The CommandText doesn't have parameters that match the parameter names you're adding. Instead, you've hard-coded
VALUES ('Ana', '2', '2', '2', 'Both' )
Instead, try:
VALUES (@tax_catagory, @tax_rate, @ed_cess_rate, @s_ed_cess_rate, @tax_on)