.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 30th, 2009, 10:12 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
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.

Reply With Quote
  #2  
Old July 2nd, 2009, 08:37 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
Quote:
Originally Posted by zeetec1
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.


In the default code behind page (.cs page):
Code:
NWindUtil NW = new NWindUtil;
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();
__________________
Roger (.NET MCP)

Reply With Quote
  #3  
Old July 2nd, 2009, 08:39 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
In the default code behind page (.cs page):
Code:
NWindUtil NW = new NWindUtil;
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();


Great I'll give it a try.

Thanks.

Reply With Quote
  #4  
Old July 3rd, 2009, 04:07 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
In the default code behind page (.cs page):
Code:
NWindUtil NW = new NWindUtil;
DataList1.DataSource = NW.GetCustomers();
DataList1.DataBind();


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 for my customers.aspx file is shown below:-

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.

Reply With Quote
  #5  
Old July 3rd, 2009, 08:16 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
Quote:
Originally Posted by zeetec1
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 for my customers.aspx file is shown below:-

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.


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).

Reply With Quote
  #6  
Old July 3rd, 2009, 08:22 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
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).


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.

Reply With Quote
  #7  
Old July 3rd, 2009, 08:29 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
Quote:
Originally Posted by zeetec1
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.


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.

Reply With Quote
  #8  
Old July 3rd, 2009, 08:33 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
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.


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.

Reply With Quote
  #9  
Old July 3rd, 2009, 08:43 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
Quote:
Originally Posted by zeetec1
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.


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;";
        }


?

Reply With Quote
  #10  
Old July 3rd, 2009, 08:44 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
I can write it up using the sql driver and drop that code here.

Reply With Quote
  #11  
Old July 3rd, 2009, 08:56 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
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.

Reply With Quote
  #12  
Old July 3rd, 2009, 09:39 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
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.


Ok thanks for that, I'll give it another try.

Reply With Quote
  #13  
Old July 3rd, 2009, 10:03 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
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.


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.

Reply With Quote
  #14  
Old July 3rd, 2009, 10:14 AM
rclark's Avatar
rclark rclark is offline
I do .NET for a living
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Florida
Posts: 1,434 rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level)rclark User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 25 m 53 sec
Reputation Power: 157
Quote:
Originally Posted by zeetec1
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.


the codebehind is incorrect:
CodeBehind="Default.aspx.cs"
should be:
CodeBehind="Customers.aspx.cs"

Reply With Quote
  #15  
Old July 3rd, 2009, 10:32 AM
zeetec1 zeetec1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 215 zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level)zeetec1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 20 h 56 m 57 sec
Reputation Power: 4
Quote:
Originally Posted by rclark
the codebehind is incorrect:
CodeBehind="Default.aspx.cs"
should be:
CodeBehind="Customers.aspx.cs"


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?

Reply With Quote
Reply

Viewing: ASP Free ForumsProgramming.NET Development > ASP.Net/C# - Displaying list of data from DB in a DataList


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT