|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
ASP.Net/C# - Displaying list of data from DB in a DataList
Hi,
I'm new to .net and am trying to display a list of data from my Database in a DataList. I need to display a list of my data using a function called Get Customers. The code for this function and it's surrounding code is shown below:- Code:
public class NWindUtil
{
public static DataSet GetCustomers()
{
string SelectCmdString = "select * from customers";
OdbcConnection myConnection = new OdbcConnection(GetConnectionString());
OdbcDataAdapter mySqlDataAdapter = new OdbcDataAdapter(SelectCmdString, myConnection);
DataSet myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet);
return myDataSet;
}
public static string GetConnectionString()
{
return @"Driver={Microsoft Access Driver (*.mdb)}; Dbq=C:\Inetpub\wwwroot\NWind\db\nwind2003.mdb;";
}
public static DataSet GetOrders(int CustomerId)
{
return new DataSet();
}
}
}
So I need to connect my DataList on my default.aspx page to this GetCustomer funtion in order to display my data in my DataList. How can I do this? Any help most appreciated. |
|
#2
|
||||
|
||||
|
Quote:
In the default code behind page (.cs page): Code:
NWindUtil NW = new NWindUtil; DataList1.DataSource = NW.GetCustomers(); DataList1.DataBind();
__________________
Roger (.NET MCP) |
|
#3
|
|||
|
|||
|
Quote:
Great I'll give it a try. Thanks. |
|
#4
|
|||
|
|||
|
Quote:
Hi, I tried your code and it's giving me a few errors. The first is a compilation error for my customers.aspx file:- Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0117: 'ASP.customers_aspx' does not contain a definition for 'mySqlDataAdapter' Source Error: Code:
Line 26: Line 27: Line 28: <asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#336666" Line 29: BorderStyle="Double" BorderWidth="3px" CellPadding="4" OnLoad="mySqlDataAdapter" Line 30: GridLines="Horizontal" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" DataSourceID="SqlDataSource1"> Code:
<%@ Page language="c#" Codebehind="Customers.aspx.cs" AutoEventWireup="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script runat="server">
</script>
<html>
<head>
<title>Customers</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="Form1" method="post" runat="server">
Home Page >> Customers
<br>
<br>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Ref_PracticeConnectionString %>"
SelectCommand="SELECT [Forename], [Surname] FROM [admin_users]"></asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#336666"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" OnLoad="mySqlDataAdapter"
GridLines="Horizontal" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" DataSourceID="SqlDataSource1"> <FooterStyle BackColor="White" ForeColor="#333333" />
<ItemStyle BackColor="White" ForeColor="#333333" />
<SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
Forename:
<asp:Label ID="ForenameLabel" runat="server" Text='<%# Eval("Forename") %>'></asp:Label><br />
Surname:
<asp:Label ID="SurnameLabel" runat="server" Text='<%# Eval("Surname") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
I have highlighted the code which is causing the compilation error above. The second problem I was having was a few errors with my .cs file which were:- Invalid token '(' in class, struct or interface member declaration Invalid token '(' in class, struct or interface member declaration Invalid token '=' in class, struct or interface member declaration My code looks like this:- Code:
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NWind;
using System.Data.Odbc;
namespace NWind
{
public partial class Login : System.Web.UI.Page
{
NWindUtil NW = new NWindUtil();
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();
}
}
There is a red squigly line under the red highlighted code above which is causing these errors. Thanks. Last edited by zeetec1 : July 3rd, 2009 at 04:43 AM. |
|
#5
|
||||
|
||||
|
Quote:
Sorry, I did the test code in VB.NET. At the top of the .cs file for the NWindUtil class, put a using statement for the SQL Client: using System.Data.SqlClient; Try that first and report back the results. If that doesn't solve both issues, I'll write it up in C# for you (I program in both). |
|
#6
|
|||
|
|||
|
Quote:
Hi, Just added that System.Data.SqlClient in but it hasn't made a difference. The same errors are showing as in my previous post. |
|
#7
|
||||
|
||||
|
Quote:
Yeah, just figured that out, you're not using SQL Server. I had converted your code to use SQL for my testing. I'll see if I can get it working with your code and post back the results. |
|
#8
|
|||
|
|||
|
Quote:
I am using SQL Server 2005 as my backend database sits there. I need the data to be pulled from my SQL Server database and displayed in my DataList. |
|
#9
|
||||
|
||||
|
Quote:
Then why aren't you using the native sql driver instead of the ODBC driver and why is your connection string a Microsoft Access driver string? Code:
public static string GetConnectionString()
{
return @"Driver={Microsoft Access Driver (*.mdb)}; Dbq=C:\Inetpub\wwwroot\NWind\db\nwind2003.mdb;";
}
? |
|
#10
|
||||
|
||||
|
I can write it up using the sql driver and drop that code here.
|
|
#11
|
||||
|
||||
|
Ok, here is my simple solution returing just the company name to the page (though all fields are available):
default.aspx code: Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblCompanyName" Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>'>
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></div>
</form>
</body>
</html>
default.aspx.cs code behind page: Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
NWUtil NW = new NWUtil();
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();
}
}
}
}
NWUtil.cs class page: Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication1
{
public class NWUtil
{
public DataSet GetCustomers() //not a static function
{
SqlConnection myConnection = new SqlConnection("Data Source=roger;Initial Catalog=NorthWind;Persist Security Info=False;User ID=youruseridhere;pwd=yourpasswordhere;pooling=tru e; Max Pool Size=100;");
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "Select * from customers";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = myCommand;
DataSet myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet);
return myDataSet;
}
}
}
The above code functions properly on my system using ASP.NET ver 2005. I'm using SQL Server 2000, but the syntax is identical. |
|
#12
|
|||
|
|||
|
Quote:
Ok thanks for that, I'll give it another try. |
|
#13
|
|||
|
|||
|
Quote:
I've almost got that working now but it's coming up with a Parse error:- Server Error in '/' Application. -------------------------------------------------------------------------------- Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: Could not load type 'WebApp1.Customers.aspx.cs'. It's pointing to this line in Customer.aspx:- Code:
Line 1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JWebApp1.Customers.aspx.cs" %> Thanks. |
|
#14
|
||||
|
||||
|
Quote:
the codebehind is incorrect: CodeBehind="Default.aspx.cs" should be: CodeBehind="Customers.aspx.cs" |
|
#15
|
|||
|
|||
|
Quote:
Sorry it's brought up a few more errors I'm afraid:- The Name 'Datalist1' does not exist in the current context This is pointing to:- Code:
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();
and the error:- The type of namespace name 'NWUtil' could not be found (are you missing a using directive or an assembly reference?) Any ideas why I'm getting these errors? |
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > ASP.Net/C# - Displaying list of data from DB in a DataList |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|