6 Replies - 6501 Views - Last Post: 18 April 2013 - 12:45 AM Rate Topic: -----

#1 david079  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-October 11

ms access table data in gridview

Posted 04 October 2011 - 10:49 PM

I have a simple windows form application in vb.net ,now I am trying to use the code in asp.net web form .
I have been able to insert textbox text into ms access database table but i cannt display the gridview on web form and retrive data from the table .
I have just gotten into asp.net and have tried few ways to do this but dont understand well enough.
if someone could show me some code that i could add to my form i would appreciate it
Thanks

Imports System.Data.OleDb 
Public Class WebForm1 
    Inherits System.Web.UI.Page 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
 
    End Sub 
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\testdb2.accdb") 
        Dim CmdStr As String = "insert into sample (emp_name,category,city) values ('" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')" 
        con.Open() 
        Dim cmd As OleDbCommand = New OleDbCommand(CmdStr, con) 
        cmd.ExecuteNonQuery() 
        con.Close() 
        MsgBox("Done") 
 
    End Sub 
 
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click 
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\testdb2.accdb") 
        Dim Query2 As String = "select * from Sample" 
        Dim Da As OleDbDataAdapter, Ds As New DataSet, Dtb As New System.Data.DataTable 
        con.Open() 
        Da = New OleDbDataAdapter(Query2, con) 
        Da.Fill(Ds) 
        con.Close() 
        Dtb = Ds.Tables(0) 
        GridView1.DataSource = Dtb 
        GridView1.DataBind()
 
    End Sub 
End Class.




Is This A Good Question/Topic? 1
  • +

Replies To: ms access table data in gridview

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: ms access table data in gridview

Posted 04 October 2011 - 11:02 PM

So are you getting any problem with the current code? Is there any error message? or what is happening to it?
Was This Post Helpful? 1
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: ms access table data in gridview

Posted 05 October 2011 - 06:46 AM

I'm not sure if you're having problems with inserting data into the database or retrieving the data from the database.

If you open your database, are there records in there with the data provided by the end user through your application?

If it is there... have you tried placing a break point in your code to see if you actually retrieve the data from your database?

If you're getting any error messages, please post them so that we can get a better understanding of what your problem is.


-Frinny
Was This Post Helpful? 0
  • +
  • -

#4 david079  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-October 11

Re: ms access table data in gridview

Posted 05 October 2011 - 09:53 PM

Thanks for response .I am not getting any error message when i run the code.On page load,the form does not display the gridview only the text boxes , other than this i have no problem inserting data into the database table.I do not know if i have to add any tags on the aspx page.any bit of info you can give would be very helpful. Thanks all!
Was This Post Helpful? 0
  • +
  • -

#5 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: ms access table data in gridview

Posted 06 October 2011 - 06:51 AM

Well, according to your code you have to hit Button2 in order to display the data...
If this isn't what you want, and you want to show it when the page loads, then you have to move that code to the Page Load event.

Don't call the GridView.DataBind method in the Page Load if you are going to allow the user to edit data within the grid. If you do, then you'll replace anything that the user provided with the original data and you won't get anything that the user provided. So, I recommend calling the GridView.DataBind method in the Page PreRender event (since this event happens last in the asp.net page lifecycle).

-Frinny
Was This Post Helpful? 1
  • +
  • -

#6 david079  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-October 11

Re: ms access table data in gridview

Posted 07 October 2011 - 10:46 PM

HI
Thank you so much . I got it to work now
Was This Post Helpful? 0
  • +
  • -

#7 hoodh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 13

Re: ms access table data in gridview

Posted 18 April 2013 - 12:45 AM

OMG... I was also having this problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1