Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I create a project. *I want to when I run the form2 form menu bar record will show automatically.
access database
vb.net 2010
connecting through coding
use connecting module
------------------------------------
in form load event
call connection
call showdata '---*I want to when I run the form2 form menu bar record will show automatically.
----------------------------------------------
call showdata coding

VB
dim ds as new dataset
dim dt as new datatable
ds.table.add(dt)
dim da as new oledbdataadapter
da=new oledbdataadapter("select * from company", cn)' here cn =connection
da.fill(dt)
text1.text=dt.rows(0).item(0)
text2.text=dt.rows(0).item(1)


Coding showing data perfect after add data

But when i other any thing change coding (for exampl. i add a text box for showing the datetime) and save then run the application and goto the form2 all records are delete automatically. then I add record and stop application then run 100 times work properly But when I press the save button after delete/add some blank space of coding window the when I run the application and opened the form2 there are no record. Please help me

What I have tried:

dim ds as new dataset
dim dt as new datatable
ds.table.add(dt)
dim da as new oledbdataadapter
da=new oledbdataadapter("select * from company", cn)
da.fill(dt)
text1.text=dt.rows(0).item(0)
text2.text=dt.rows(0).item(1)
Posted
Comments
[no name] 15-May-17 6:58am    
You are probably copying over the database file. Learn how to debug your code and find out what is going on.
Jayanta Modak 15-May-17 7:30am    
i am beginner in vb.net please help me what is the wrong in my code
[no name] 15-May-17 11:12am    
Learn how to debug your code and find out what is going on. We can't debug your code for you, we don't have your code. We can't tell you what is happening with your database, because we don't have it. It's up to you.
Jayanta Modak 16-May-17 2:47am    
Imports System.Data.OleDb
Module Modconnection
Public cnn, cn As New OleDb.OleDbConnection
Public cm As New OleDb.OleDbCommand
Public dr As OleDbDataReader

Public Sub connection()
cn = New OleDb.OleDbConnection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb;Persist Security Info=False;"
.Open()
End With
End Sub

Add Data button-----------------------------------------

Call connection()
Try

cm = New OleDb.OleDbCommand
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "INSERT INTO cominfo (Comname,Add1,Add2,Phone,Vat,cst_tin,Email,Website) VALUES (@Comname,@Add1,@Add2,@Phone,@Vat,@cst_tin,@Email,@Website)"

.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Comname", System.Data.OleDb.OleDbType.VarChar, 255, Me.ComnameTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Add1", System.Data.OleDb.OleDbType.VarChar, 255, Me.Add1TextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Add2", System.Data.OleDb.OleDbType.VarChar, 255, Me.Add2TextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Phone", System.Data.OleDb.OleDbType.VarChar, 255, Me.PhoneTextBox.Text))

.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Vat", System.Data.OleDb.OleDbType.VarChar, 255, Me.VatTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@cst_tin", System.Data.OleDb.OleDbType.VarChar, 255, Me.Cst_tinTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Email", System.Data.OleDb.OleDbType.VarChar, 255, Me.EmailTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Website", System.Data.OleDb.OleDbType.VarChar, 255, Me.WebsiteTextBox.Text))


' RUN THE COMMAND
cm.Parameters("@Comname").Value = Me.ComnameTextBox.Text
cm.Parameters("@Add1").Value = Me.Add1TextBox.Text
cm.Parameters("@Add2").Value = Me.Add2TextBox.Text
cm.Parameters("@Phone").Value = Me.PhoneTextBox.Text

cm.Parameters("@Vat").Value = Me.VatTextBox.Text
cm.Parameters("@cst_tin").Value = Me.Cst_tinTextBox.Text
cm.Parameters("@Email").Value = Me.EmailTextBox.Text
cm.Parameters("@Website").Value = Me.WebsiteTextBox.Text

cm.ExecuteNonQuery()
MsgBox("Record saved.", MsgBoxStyle.Information)


Me.ComnameTextBox.Text = ""
Me.Add1TextBox.Text = ""
Me.Add2TextBox.Text = ""
Me.PhoneTextBox.Text = ""

Me.VatTextBox.Text = ""
Me.Cst_tinTextBox.Text = ""
Me.EmailTextBox.Text = ""
Me.WebsiteTextBox.Text = ""



Exit Sub
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Richard Deeming 16-May-17 11:30am    
Looks like you've got DB.accdb defined in your project, and set to copy to the output directory.

Select the file in Solution Explorer, and look at the properties. Set the "Copy to output directory" property to "Copy if newer".

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