By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

About the Data Grid View in VB.NET

Ajay Bhalala
100+
P: 119
I am Using the MS Access database as backend tool.
I have the question which I want to do is as follows...

Q : Create a database "Student" which consist of following fields :
• id • name • c • c++ • vb • dtp • sad • ds • oa • gender
(Note : c,c++,vb,dtp,sad,ds and oa are the fields for store the marks of that 7 subjects.)

I want to perform following operations on button_click:
Insert, Update, and Delete

And also view the data in grid view after every operations.


I have write the code for this as following...
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form1
  4.  
  5.     Dim con As New OleDbConnection("Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\SY BSC IT\VB.NET\Student.mdb")
  6.     Dim qry As String
  7.  
  8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.         'TODO: This line of code loads data into the 'StudentDataSet1.stud' table. You can move, or remove it, as needed.
  10.         Me.StudTableAdapter.Fill(Me.StudentDataSet1.stud)
  11.     End Sub
  12.  
  13.     Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
  14.         qry = "delete from stud where id = " & TextBox8.Text & " "
  15.         Dim cmd As New OleDbCommand(qry, con)
  16.         Try
  17.             con.Open()
  18.             cmd.ExecuteNonQuery()
  19.             MsgBox("Deleted!")
  20.             con.Close()
  21.         Catch ex As Exception
  22.             con.Close()
  23.             MsgBox("Not Deleted!")
  24.         End Try
  25.     End Sub
  26.  
  27.     Private Sub btnIns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIns.Click
  28.         Try
  29.             con.Open()
  30.             MsgBox("Connection State : " & con.State)
  31.             qry = "insert into stud values('" & TextBox8.Text & "', ' " & TextBox9.Text & " ', ' " & TextBox1.Text & " ', ' " & TextBox2.Text & " ', ' " & TextBox3.Text & " ', ' " & TextBox4.Text & " ', ' " & TextBox5.Text & " ', ' " & TextBox6.Text & " ', ' " & TextBox7.Text & " ')"
  32.             Dim cmd As New OleDbCommand(qry, con)
  33.             cmd.ExecuteNonQuery()
  34.             MsgBox("Data Submitted!")
  35.             con.Close()
  36.         Catch ex As Exception
  37.             con.Close()
  38.             MsgBox("Not Submitted")
  39.         End Try
  40.     End Sub
  41.  
  42.     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  43.         qry = "update stud set  name = '" & TextBox9.Text & "' where id = " & TextBox8.Text & " ', c=" & TextBox1.Text & "', c++=" & TextBox2.Text & "', vb=" & TextBox3.Text & "', dtp=" & TextBox4.Text & "', sad=" & TextBox5.Text & "', ds=" & TextBox6.Text & "', oa=" & TextBox7.Text & "'"
  44.         Dim cmd As New OleDbCommand(qry, con)
  45.         Try
  46.             con.Open()
  47.             cmd.ExecuteNonQuery()
  48.             MsgBox("Updated!")
  49.             con.Close()
  50.         Catch ex As Exception
  51.             con.Close()
  52.             MsgBox("Not Updated!")
  53.         End Try
  54.     End Sub
  55. End Class
  56.  
There is no error occurred, when I run the form,Delete operation is done successfully, But Insert and Update operations are not done. Which changes I have to do in this code for successful Insert and Update operations?

And I have another problem...
User have to enter the required fields in the form. After entering the all content, User can click on Insert or Update or Delete buttons.

After clicking any of these 3 buttons, the changes should be display in DataGridView in vb.net form. But changes are not displayed in the GridView. What can I do for displaying changes in grid view? in above code which changes I have to do for displaying the changes in grid view?
Nov 25 '14 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
There is no error occurred, ..., But Insert and Update operations are not done....
Did you check the database to see if a record was inserted?


You should be using the OleDbCommand.Parameters Property (see the link for documentation and examples) to avoid sql insertion attacks and make your code more legible (textbox1,textbox2 etc make no sense to someone reading your code because they have no idea what textbox1 represents...a better name for textbox1 would be something like "PersonNameInput" or something more descriptive of what it is)

ANYWAY, you have this:
Expand|Select|Wrap|Line Numbers
  1. insert into stud values('" & TextBox8.Text & "', ' " & TextBox9.Text & " ', ' " & TextBox1.Text & " ', ' " & TextBox2.Text & " ', ' " & TextBox3.Text & " ', ' " & TextBox4.Text & " ', ' " & TextBox5.Text & " ', ' " & TextBox6.Text & " ', ' " & TextBox7.Text & " ')"
Which, for the most part looks fine...(even though you should be using parameters). So I recommend that you run the code and then check the database to see if it is there.

It is highly likely that you are inserting the row but not updating the grid that displays the information to the user with the updated information. Likewise, I think the same thing is happening after you do your Update command.

(To refresh the data, run the code that you have on line 10 in the code you posted again... you may also have to set the data source of the grid to the result of that code as well...)
Nov 25 '14 #2

Ajay Bhalala
100+
P: 119
Ok I will try as you say.
Nov 26 '14 #3

P: 1
It is highly likely that you are inserting the row but not updating the grid that displays the information to the user with the updated information. Likewise, I think the same thing is happening after you do your Update command.
Nov 28 '14 #4

Ajay Bhalala
100+
P: 119
Ok thank you for the help. Now it's works.
Nov 29 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.