views:

43

answers:

2

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][1]

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()
+2  A: 

You're calling:

sqlcon.open

before:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

That's where your null ref is coming from. You need to "new" sqlcon before calling open on it.

Try this:

sqlcon = New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

sqlcon.open

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()

It's tougher to help you with the connection string since we don't know your environment. But here is a link to a great reference: http://connectionstrings.com/

Paul Sasik
Is it possible to connect even if I did not add a data source in vb.net?
Absolutely. i'm assuming you're talking about the DataSource object in the designer? If so, then it's just one way of connecting to data... "visually" MS makes this very easy but you'll do better staying in "raw" code like you have thus far. BUT, for getting started, the DataSource is an very easy way to go.
Paul Sasik
+1  A: 

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:

Using sqlcon As New SqlConnection("Data Source=SENBONZAKURA\SQLEXPRESS;Initial Catalog=testing;User ID=SenbonZakura/Rew;")

    sqlcon.Open()
    Dim sqlcom As New SqlCommand()
    sqlcom.Connection = sqlcon

    sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (@ParameterID, @ParameterLastName, @ParameterFirstName, @ParameterSchool)"

    sqlcom.Parameters.AddWithValue("@ParameterID", TextBox1.Text)
    sqlcom.Parameters.AddWithValue("@ParameterLastName", TextBox2.Text)
    sqlcom.Parameters.AddWithValue("@ParameterFirstName", TextBox3.Text)
    sqlcom.Parameters.AddWithValue("@ParameterSchool", TextBox4.Text)

    sqlcom.ExecuteNonQuery()

End Using
  • EDIT

Note that I´ve omitted "select * from [student]", you didn´t use this query!

Javier Morillo
-1: SqlCommand implements IDisposable as well.
John Saunders
oops! True. Thanks John, I´ll take note :-)
Javier Morillo