Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi programmers out there, I have a windows form with three textboxes namely
Id, name,age . I also added a gridview to the form and its bound to the database.
I also added three buttons one for insert, delete and update records. Insert is done through the textboxes, upon a click of button am able to see what I have added in gridview. Am also able to delete upon a click of a button.

The code for update which is not working, no error message;


Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Collections.Generic
Imports System.ComponentModel

Public Class sss
    Inherits System.Windows.Forms.Form
    Dim con As OleDbConnection = Nothing
    Dim ds As DataSet
    Dim cmd As OleDbCommand = Nothing
    Dim dt As New DataTable
    Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb;Persist Security Info=False;"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ID, FName, Age As String
ID = Txtid.Text
FName = txtfullname.Text
Age = txtAge.Text
Dim con As New OleDbConnection(cs)
        Dim cmd As New OleDbCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Update ssss SET FName=@FName,Age=@Age WHERE ID=@ID"
        cmd.Connection = con
        con.Open()
        cmd.Parameters.AddWithValue("@ID", ID)
        cmd.Parameters.AddWithValue("@FName", FName)
        cmd.Parameters.AddWithValue("@Age", Age)
cmd.ExecuteNonQuery()
        MessageBox.Show("updated......")

        con.Close()
        Binder()
        	
End Sub
Private Sub Binder()
        Dim con As New OleDbConnection(cs)
        Dim cmd As New OleDbCommand()
        cmd.Connection = con
        con.Open()
        Dim da As New OleDbDataAdapter("select * from ssss", con)
        Dim dt As New DataTable()
        da.Fill(dt)
        DataGridView1.DataSource = dt
        con.Close()
    End Sub
Posted
Updated 25-Sep-13 5:23am
v2
Comments
[no name] 25-Sep-13 11:16am    
Well first thing is, in your SQL you define a parameter "@FName" but in your parameter list you called it "@Name". How is it that you know it "is not working"? What is not working?
mayeso 25-Sep-13 11:25am    
A record is not updated either in the database or on gridview. updated the question
[no name] 25-Sep-13 11:32am    
So have you run your code under a debugger to find out what is going on? Is your button click event firing at all? Did you try running the query in your database to see if it works there? Is ID a string in your database table? Or is it really supposed to be an integer?
mayeso 25-Sep-13 11:35am    
id is a text data type in access, the button is firing, because a message updated is showing but no record is updated
mayeso 25-Sep-13 11:36am    
the query works great in database

1 solution

Your parameter is "@FName" in your sql statement while it says "@Name" when you add the parameter with value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900