I have created a small table of student info in MS Access 2007. The table name is “stdinfo” and fields are rollno,sname,sfather,address.
Now i want to display the table fields in asp.net application.
code behind code is
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:|Users|mac|Desktop|db.accdb;";
OleDbConnection cn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from stdinfo";
cmd.Connection = cn;
cn.Open();
rolllbl.Text = Convert.ToString(cmd.ExecuteNonQuery());
cn.Close();
}
and aspx code is
<p>
<asp:Label ID="rolllbl" runat="server" Text="RollNo."></asp:Label>
</p>
<p>
<asp:Label ID="namelbl" runat="server" Text="Name"></asp:Label>
</p>
<p>
<asp:Label ID="fnamelbl" runat="server" Text="Fname"></asp:Label>
</p>
<p>
<asp:Label ID="addlbl" runat="server" Text="Address"></asp:Label>
</p>
I did not wish to write the connection string in web config file. I am using VS 2010.
Is there any thing i need to add in the code behind ? the code is not working…
You have to change your code.
ExecuteNonQuerymethod returns the no. of rows affected, You will not get anything by converting into a string.Also you can also use a gridview control