473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

About the Data Grid View in VB.NET

Ajay Bhalala
119 64KB
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
4 1995
Frinavale
9,735 Expert Mod 8TB
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
119 64KB
Ok I will try as you say.
Nov 26 '14 #3
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
119 64KB
Ok thank you for the help. Now it's works.
Nov 29 '14 #5

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

Similar topics

2
by: Joe Griffith | last post by:
I'm using a Win Forms Data Grid View control in unbound mode. When I add columns using the wizard the first item is the column name. Everything works fine. However, if you return to the...
0
by: Gian Paolo | last post by:
this is something really i can't find a reason. I have a form with a tabcontrol with tree pages, in the second page there is a Data GRid View. Plus i have a class. When i open the form i...
0
by: nanaalwi | last post by:
hi there, im doing a project right now to download a data from EEPROM of a camcorder. i used a 'data grid view' to display all the data downloaded from the camcorder. however, i have to save the...
1
by: dineshsanduke | last post by:
hi i m new user i created programatic data grid. i want numeric cell validation in data grid view
2
by: jakeesgirl | last post by:
I'm creating a C# program in Visual Studio. When I query the sql database, the results are put into a data grid view. However, the column widths are always too small when displayed. I have text that...
0
by: cadab | last post by:
I have a data grid view, i have specified several columns through the designer that i would like to show on the grid, i have mapped them to the datasource, this works fine, the problem is, my data...
1
by: mcupito | last post by:
I am trying to populate a data grid view based off of two text boxes, Start date and End date. I am not sure how to work with DataGridViews or DataSets. So if textbox1.text = x/x/2013 and...
0
by: jaseem0712 | last post by:
I have a data grid view like this: http://bytes.com/attachment.php?attachmentid=7458&stc=1&d=1390739933 i am loading data grid view like this: Dim cd As SqlCommandBuilder = New...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.