Popular Gridview with Access Data

Asked

Viewed 71 times

0

I managed to do this in VB.NET, but now I need to do it in C#and I can’t turn the code the right way.

I have this Gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" >
    <Columns>
        <asp:BoundField DataField="BatchID" HeaderText="BatchID" InsertVisible="False" ReadOnly="True" SortExpression="BatchID" />
        <asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" />
        <asp:BoundField DataField="BatchSize" HeaderText="BatchSize" SortExpression="BatchSize" />
        <asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />
        <asp:BoundField DataField="StartReq" HeaderText="StartReq" SortExpression="StartReq" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
    </Columns>
</asp:GridView>

The data I put in it, I bring through an Access bank that I created. In VB.NET I managed to make it work, but I need to make this code work on C# now.

This is the code on VB.NET that I need to pass on to C#

Private Sub GridView1_LoadData(sender As Object, e As EventArgs) Handles Me.Load
        Dim dtAllBatches As New Northwind.AllBatchesDataTable
        Using da As New NorthwindTableAdapters.AllBatchesTableAdapter
            da.Fill(dtAllBatches)
        End Using
            GridView1.DataSource = dtAllBatches.DefaultView
            GridView1.DataBind()
End Sub  

I was doing in VB.NET, but I was wrong and it was meant to be done in C#. I tried to find a way to make it work on the Internet, but I couldn’t find something to help me and at least understand how I should do it.

1 answer

0

I hope it helps.

string strProvider = "@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ArquivoAcess.accdb";
string strSql = "Select * from [Nome_Tabela]";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable tabelaTmp = new DataTable();
da.Fill(tabelaTmp);
dataGridView1.DataSource = tabelaTmp;
  • I used the way you told me, but it just went wrong. It didn’t work.

  • What mistake you made ?

  • This: Unrecognized escape Sequence ; . But I can’t understand why, being that it has at the end of the line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.