Could someone please help me in here, I’m just a beginner who want to learn on manipulating SQL Server data using vb.net. I have already experienced manipulating data in ms access. So I just recycled the code that I used in here. But unfortunately I got this error:
Object reference not set to an instance of an object.
And another problem is that, I’m not really sure if what I have inputted in the SqlConnection matches what is in SQL Server. Here is a screen shot of sql server management studio express:
[http://screencast.com/t/Y2Q0NWRhYTA%5D%5B1%5D
Imports system.data.sqlclient
'declarations
Dim idnum As String
Dim lname As String
Dim fname As String
Dim skul As String
Dim sqlcon As SqlConnection
Dim sqlcom As SqlCommand
idnum = TextBox1.Text
lname = TextBox2.Text
fname = TextBox3.Text
skul = TextBox4.Text
sqlcon.open
sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")
sqlcom = New SqlCommand("select * from [student]", sqlcon)
Dim strsql As String = "insert into [student]([ID], [LASTNAME], [FIRSTNAME], [SCHOOL]) values('" + idnum + "','" + lname + "','" + fname + "','" + skul + "')"
sqlcom.ExecuteNonQuery()
Paul is right, but in addition to Paul answer, since SqlConnection implements interface IDisposable, I suggest you to enclosure it in a using statement. See more about using in this post
Another important suggestion is to use parameters to pass the values to your query. It gives you security, the code is simplier…
The complete code would be something like this:
Note that I´ve omitted
"select * from [student]", you didn´t use this query!