Nasser
Nasser

Reputation: 2140

Insert data into SQL database in VB.NET

I am trying to insert Supplier data into SQL database which I built inside VB.NET.

Here is the code that I am using:

Dim myconnect As New SqlClient.SqlConnection
        myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True"


        Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
        mycommand.Connection = myconnect
        mycommand.CommandText = "INSERT INTO Supplier (SupplierID, Name, Email,Phone,Address,City) VALUES (@SID, @Name, @Email, @Phone, @Address, @City)"
        myconnect.Open()

        Try
            mycommand.Parameters.Add("@SID", SqlDbType.Int).Value = SIDTextBox.Text
            mycommand.Parameters.Add("@Name", SqlDbType.NVarChar).Value = NameTextBox.Text
            mycommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text
            mycommand.Parameters.Add("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text
            mycommand.Parameters.Add("@Address", SqlDbType.NVarChar).Value = AddressTextBox.Text
            mycommand.Parameters.Add("@City", SqlDbType.NVarChar).Value = CityTextBox.Text
            mycommand.ExecuteNonQuery()
            MsgBox("Success")
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message)
        End Try
        myconnect.Close()

The problem when I enter the data, I got the Success message but when I check the database, I can't find the data !!

I checked the mdf file and I found it correct (The same one that I am using)

Any help please.

Upvotes: 2

Views: 57118

Answers (3)

Shadow
Shadow

Reputation: 1

I had a similar problem and in my case the application didn't have write access to the folder where the database file was.

Upvotes: -2

sentil kumar
sentil kumar

Reputation: 95

Dim query as String = String.Empty
query = "INSERT INTOSupplier (SupplierID, Name, Email,Phone,Address,City) VALUES (@SID, @Name, @Email, @Phone, @Address, @City) "   
Using conn as New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True")
    Using comm As New SqlCommand()
        With comm
            .Connection = conn
            .CommandType = CommandType.Text
            .CommandText = query
            mycommand.Parameters.Add("@SID", SqlDbType.Int).Value = SIDTextBox.Text
        .Parameters.Add("@Name", SqlDbType.NVarChar).Value = NameTextBox.Text
        .Parameters.Add("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text
        .Parameters.Add("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text
        .Parameters.Add("@Address", SqlDbType.NVarChar).Value = AddressTextBox.Text
        .Parameters.Add("@City", SqlDbType.NVarChar).Value = CityTextBox.Text            
        End With
        Try
            conn.open()
            comm.ExecuteNonQuery()
        Catch(ex as SqlException)
            MessageBox.Show(ex.Message.ToString(), "Error Message")
        End Try
    End Using
End USing 

Try this out.

Upvotes: 2

Nasser
Nasser

Reputation: 2140

I got it

The problem was with this line:

myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB\InvDB.mdf;Integrated Security=True;User Instance=True"

I replaced |DataDirectory| with the path of the InvDB.mdf and it worked

Upvotes: 2

Related Questions