Question execute non query error...

teroy

New member
Joined
Feb 16, 2010
Messages
2
Programming Experience
Beginner
im trying to a update a record in the the database.. but i got an execute no query error
heres the code:
VB.NET:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim da As New OleDb.OleDbDataAdapter
        Dim ds As New DataSet
        Dim obj As New OleDb.OleDbCommand

        CNN.Open()

        sql = "SELECT *FROM tblStudent WHERE studno = '" & txtStudno.Text & "'"

        da = New OleDb.OleDbDataAdapter(sql, CNN)

        da.Fill(ds, "stud")

        obj = New OleDb.OleDbCommand

        If ds.Tables("stud").Rows.Count > 0 Then
            sql = "UPDATE tblStudent SET studno = '" & txtStudno.Text & "', lname = '" & txtLname.Text & "', fname = '" & txtFname.Text & "', mi = '" & txtMi.Text & "', yr = '" & cbYear.Text & "', section = '" & Sections.Text & "', contactnum = '" & txtContactNo.Text & "' WHERE studno = '" & txtStudno.Text & "'"

        Else
            sql = "INSERT INTO tblStudent(studno,lname,fname,mi,yr,section,contactnum)VALUES('" & txtStudno.Text & "', '" & txtLname.Text & "','" & txtFname.Text & "','" & txtMi.Text & "','" & cbYear.Text & "', '" & Sections.Text & "', '" & txtContactNo.Text & "')"
        End If

        obj.Connection = CNN
        obj.CommandType = CommandType.Text
        obj.CommandText = sql
        obj.ExecuteNonQuery()

        CNN.Close()
        MessageBox.Show("Student record successfully updated!", "SJA", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call clear()
        Call fill_lstStudent()
    End Sub
help!!! :(
 
Last edited by a moderator:
1. Please post in the most appropriate forum, not just the first one you come to. Thread moved.

2. Please wrap your code snippets in
VB.NET:
 tags.  There's a button on the advanced editor.

3. When there's an error, there's an error message.  Please ALWAYS provide it.

4. Regardless of any other issues, you should look at using parameters rather than string concatenation to insert values into your SQL code.  Follow the Blog link in my signature and check out my post on the topic.
 
Please follow the Dw3 tutorial in my signature, section "Creating a Simple Data App"

At the end of it (max 30 minutes of your life) you will have written 0 lines of code, and have a fully working application that reads and writes a database in the proper way..

By contrast, this code here has probably taken hours, is not good from a performance, security or object orientated perspective and doesnt work The DW3 link will start you off on the best track for doing your data access (it won't help you salvage the code youve already written; that needs dumping, I'm afraid..)
 
Back
Top