Pkrishna
Pkrishna

Reputation: 37

Variable 'std' is used before it has been assigned a value. A null reference exception could result at runtime

Why I'm gettin this kind of error. I have been practising with respect to tutorial but still I get this. how should clear this error. I have started to practice .net. so please guide me

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim std As DataSet1TableAdapters.studentTableAdapter

    If txtname.Text = "" Then
        lblMessage.Text = "Please enter name"
    ElseIf txtclass.Text = "" Then
        lblMessage.Text = "Please enter class "
    ElseIf txtsemseter.Text = "" Then
        lblMessage.Text = "Please enter semester"
    Else
        std.Insert(Name:=txtname.Text, Class:=txtclass.Text, Semester:=txtsemseter.Text)
    End If
End Sub

link of tutorial:http://www.youtube.com/watch?v=wDFo-nGdUQY

Upvotes: 0

Views: 361

Answers (2)

Christian Phillips
Christian Phillips

Reputation: 18759

You'll need to new up the variable, as in:

Dim std As New DataSet1TableAdapters.studentTableAdapter()

Edit: I was trying to do this on a phone, not easy :)

Upvotes: 0

Damith
Damith

Reputation: 63065

do as below

Dim std As New DataSet1TableAdapters.studentTableAdapter()

when you call std.Insert you need to have object of DataSet1TableAdapters.studentTableAdapter

Upvotes: 2

Related Questions