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... -
Imports System.Data.OleDb
-
-
Public Class Form1
-
-
Dim con As New OleDbConnection("Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\SY BSC IT\VB.NET\Student.mdb")
-
Dim qry As String
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
'TODO: This line of code loads data into the 'StudentDataSet1.stud' table. You can move, or remove it, as needed.
-
Me.StudTableAdapter.Fill(Me.StudentDataSet1.stud)
-
End Sub
-
-
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
-
qry = "delete from stud where id = " & TextBox8.Text & " "
-
Dim cmd As New OleDbCommand(qry, con)
-
Try
-
con.Open()
-
cmd.ExecuteNonQuery()
-
MsgBox("Deleted!")
-
con.Close()
-
Catch ex As Exception
-
con.Close()
-
MsgBox("Not Deleted!")
-
End Try
-
End Sub
-
-
Private Sub btnIns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIns.Click
-
Try
-
con.Open()
-
MsgBox("Connection State : " & con.State)
-
qry = "insert into stud values('" & TextBox8.Text & "', ' " & TextBox9.Text & " ', ' " & TextBox1.Text & " ', ' " & TextBox2.Text & " ', ' " & TextBox3.Text & " ', ' " & TextBox4.Text & " ', ' " & TextBox5.Text & " ', ' " & TextBox6.Text & " ', ' " & TextBox7.Text & " ')"
-
Dim cmd As New OleDbCommand(qry, con)
-
cmd.ExecuteNonQuery()
-
MsgBox("Data Submitted!")
-
con.Close()
-
Catch ex As Exception
-
con.Close()
-
MsgBox("Not Submitted")
-
End Try
-
End Sub
-
-
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
-
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 & "'"
-
Dim cmd As New OleDbCommand(qry, con)
-
Try
-
con.Open()
-
cmd.ExecuteNonQuery()
-
MsgBox("Updated!")
-
con.Close()
-
Catch ex As Exception
-
con.Close()
-
MsgBox("Not Updated!")
-
End Try
-
End Sub
-
End Class
-
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?
4 1995
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: -
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...)
Ok I will try as you say.
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.
Ok thank you for the help. Now it's works.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: dineshsanduke |
last post by:
hi
i m new user i created programatic data grid. i want numeric cell validation in data grid view
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |