0

I'm trying to connect to a SQL Database and read/write data to it. Some context - Visual Studio 2015, Windows 10, Connecting to SQL Server 2005 over VPN, Using ADO.Net 4.1. My code is as follows:

Imports System.Data.SqlClient

Class MainWindow

Dim con As New SqlConnection
Dim cmd As New SqlCommand


Private Sub btn_WriteInt_Click(sender As Object, e As RoutedEventArgs) Handles btn_WriteInt.Click

    Try
        'Establish connection to SQL Database
        con.ConnectionString = "Data Source=serverIP;Initial Catalog=mydatabase;Persist Security Info=True;User ID=user;Password=password"
        con.Open()
        cmd.Connection = con
        'Save data to table
        Dim int_IntTest As Integer = Int(txt_IntTest.Text)
        cmd.CommandText = "INSERT INTO tbl_SQLTest([IntTest]) VALUES('" & txt_IntTest.Text & "')"
        cmd.ExecuteNonQuery()

    Catch ex As Exception
        MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
    Finally
        con.Close()
    End Try

End Sub

Private Sub btn_TestInt_Click(sender As Object, e As RoutedEventArgs) Handles btn_TestInt.Click
    Dim int_IntTest As Integer = Int(txt_IntTest.Text)
    MsgBox(int_IntTest)
End Sub
End Class

The SQL Server table looks like this: tbl_SQLTest

When I run the application, enter "2" into txt_IntTest and click btn_WriteInt, I get the following error:

Error Message]

2
  • 2
    You aren't inserting any value for that column. Use parameters to avoid sql injection and formatting issues. Commented Jun 24, 2017 at 16:34
  • In your DBMS set a default value for that column. Then when nothing is passed your default would be used... Commented Jun 24, 2017 at 17:28

2 Answers 2

2

When you insert a new row in the table if you don't add a value for 'StringTest' column null is added, so you need to do one of these:

  • Allow null for this column un your table,
  • Pass an empty value in your query.
  • Add a default '' in the StringTest column.
Sign up to request clarification or add additional context in comments.

2 Comments

ah i see, I didnt add and name a new row. Would you mind giving me the syntax for adding and naming the row in my cmd.CommandText statement?
Yes, "INSERT INTO tbl_SQLTest (IntTest, StringTest) VALUES ('yourdata', ' ')";
0

Based on the accepted answer (which I think is the correct one and I upvoted). I recommend you to do something like this:

cmd.CommandText = "INSERT INTO tbl_SQLTest([IntTest], [StringTest) VALUES(@intTest, @stringTest)"

cmd.Parameters.AddWithValue("@intTest", Int(txt_IntTest.Text))
cmd.Parameters.AddWithValue("@stringTest", "")

cmd.ExecuteNonQuery()

1 Comment

Use Add and specify the data type... consequences could occur; for example precision...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.