472,779 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

dataset question

I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

I have a label called lblDisplay

I want the label to display the Notes column data of the selected item in
the dropdownList.

My code is causing a NullReferenceException.
Can anyone find out what is the problem please?

------------------------------------
Code: ----------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false"
Inherits="WebApplication2.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION:
absolute; TOP: 19px"
runat="server" Width="536px" Height="8px"></asp:Label>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 152px"
runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
BackColor="#003399"></HeaderStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
absolute; TOP: 48px" runat="server"
Width="272px" Height="16px" ForeColor="Red">I want to display notes of
selected employee</asp:Label>
<asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 72px"
runat="server" Width="704px" Height="56px"></asp:Label></form>
</body>
</HTML>

--------------------------------------------------code
behind--------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected DataSet ds1 = new DataSet();
protected System.Web.UI.WebControls.Label lblDisplay;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblSpecific;
protected System.Web.UI.WebControls.DropDownList ddLastName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string connectionString = "data source =localhost; initial catalog
=Northwind; integrated security=true;";
SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand sqlCommand1 = new SqlCommand();
SqlDataAdapter sda1 = new SqlDataAdapter();

// "dataset defined at class level"
// DataSet ds1 = new DataSet();

sda1.SelectCommand = sqlCommand1;
sda1.SelectCommand.Connection = sqlConnection1;
sda1.SelectCommand.CommandType = CommandType.Text;
sda1.SelectCommand.CommandText = "select * from Employees";

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

sda2.SelectCommand = sqlCommand1;
sda2.SelectCommand.Connection = sqlConnection1;
sda2.SelectCommand.CommandType = CommandType.Text;
sda2.SelectCommand.CommandText = "select * from Customers";

sda2.Fill(ds1,"Customers");

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

// to fill with specific cell data
lblSpecific.Text = "Displaying specific Data:<br><br>";
lblSpecific.Text += ds1.Tables["Employees"].Rows[0]["Notes"].ToString();

// to fill the datagrid
DataGrid1.DataSource = ds1;
DataGrid1.DataMember = "Employees";
DataGrid1.DataBind();

}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text = ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}
Nov 19 '05 #1
12 1636
The problem is that when you click the Dropdown Box, you trigger a postback.
Unfortunately, your Page_Load event only fills the dataset during the
initial GET. So there is no data to be had when you do your Autopostback.

Once a Webform is constructed and the page rendered, the object is
destroyed. You need to arrange for the data to be filled on the initial get
and saved to the cache for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

I have a label called lblDisplay

I want the label to display the Notes column data of the selected item in
the dropdownList.

My code is causing a NullReferenceException.
Can anyone find out what is the problem please?

------------------------------------
Code: ----------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION:
absolute; TOP: 19px"
runat="server" Width="536px" Height="8px"></asp:Label>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 152px"
runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
BackColor="#003399"></HeaderStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
absolute; TOP: 48px" runat="server"
Width="272px" Height="16px" ForeColor="Red">I want to display notes of
selected employee</asp:Label>
<asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 72px"
runat="server" Width="704px" Height="56px"></asp:Label></form>
</body>
</HTML>

--------------------------------------------------code
behind--------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected DataSet ds1 = new DataSet();
protected System.Web.UI.WebControls.Label lblDisplay;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblSpecific;
protected System.Web.UI.WebControls.DropDownList ddLastName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string connectionString = "data source =localhost; initial catalog
=Northwind; integrated security=true;";
SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand sqlCommand1 = new SqlCommand();
SqlDataAdapter sda1 = new SqlDataAdapter();

// "dataset defined at class level"
// DataSet ds1 = new DataSet();

sda1.SelectCommand = sqlCommand1;
sda1.SelectCommand.Connection = sqlConnection1;
sda1.SelectCommand.CommandType = CommandType.Text;
sda1.SelectCommand.CommandText = "select * from Employees";

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

sda2.SelectCommand = sqlCommand1;
sda2.SelectCommand.Connection = sqlConnection1;
sda2.SelectCommand.CommandType = CommandType.Text;
sda2.SelectCommand.CommandText = "select * from Customers";

sda2.Fill(ds1,"Customers");

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

// to fill with specific cell data
lblSpecific.Text = "Displaying specific Data:<br><br>";
lblSpecific.Text +=
ds1.Tables["Employees"].Rows[0]["Notes"].ToString();

// to fill the datagrid
DataGrid1.DataSource = ds1;
DataGrid1.DataMember = "Employees";
DataGrid1.DataBind();

}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}

Nov 19 '05 #2
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things to
set and there is no overloading of Cache.Add() that takes 1 parameter as you
wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

Once a Webform is constructed and the page rendered, the object is
destroyed. You need to arrange for the data to be filled on the initial
get and saved to the cache for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

I have a label called lblDisplay

I want the label to display the Notes column data of the selected item in
the dropdownList.

My code is causing a NullReferenceException.
Can anyone find out what is the problem please?

------------------------------------
Code: ----------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION:
absolute; TOP: 19px"
runat="server" Width="536px" Height="8px"></asp:Label>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 152px"
runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
BackColor="#003399"></HeaderStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
absolute; TOP: 48px" runat="server"
Width="272px" Height="16px" ForeColor="Red">I want to display notes of
selected employee</asp:Label>
<asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 72px"
runat="server" Width="704px" Height="56px"></asp:Label></form>
</body>
</HTML>

--------------------------------------------------code
behind--------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected DataSet ds1 = new DataSet();
protected System.Web.UI.WebControls.Label lblDisplay;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblSpecific;
protected System.Web.UI.WebControls.DropDownList ddLastName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string connectionString = "data source =localhost; initial catalog
=Northwind; integrated security=true;";
SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand sqlCommand1 = new SqlCommand();
SqlDataAdapter sda1 = new SqlDataAdapter();

// "dataset defined at class level"
// DataSet ds1 = new DataSet();

sda1.SelectCommand = sqlCommand1;
sda1.SelectCommand.Connection = sqlConnection1;
sda1.SelectCommand.CommandType = CommandType.Text;
sda1.SelectCommand.CommandText = "select * from Employees";

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

sda2.SelectCommand = sqlCommand1;
sda2.SelectCommand.Connection = sqlConnection1;
sda2.SelectCommand.CommandType = CommandType.Text;
sda2.SelectCommand.CommandText = "select * from Customers";

sda2.Fill(ds1,"Customers");

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

// to fill with specific cell data
lblSpecific.Text = "Displaying specific Data:<br><br>";
lblSpecific.Text +=
ds1.Tables["Employees"].Rows[0]["Notes"].ToString();

// to fill the datagrid
DataGrid1.DataSource = ds1;
DataGrid1.DataMember = "Employees";
DataGrid1.DataBind();

}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


Nov 19 '05 #3
Here's a little example which uses a helper function. And yes your right
about the constructor but that was not meant to be tested code just really
to point you in the right direction. In this example, the form is simply a
main switchboard which does not use any data, on the other forms in the
application they define the dataset, and adapter as class level variables
and load them from cache in the Page_Load event as described which can be
done with a simple assignment variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things
to set and there is no overloading of Cache.Add() that takes 1 parameter
as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

Once a Webform is constructed and the page rendered, the object is
destroyed. You need to arrange for the data to be filled on the initial
get and saved to the cache for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

I have a label called lblDisplay

I want the label to display the Notes column data of the selected item
in the dropdownList.

My code is causing a NullReferenceException.
Can anyone find out what is the problem please?

------------------------------------
Code: ----------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION:
absolute; TOP: 19px"
runat="server" Width="536px" Height="8px"></asp:Label>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
POSITION: absolute; TOP: 152px"
runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
BackColor="#003399"></HeaderStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
absolute; TOP: 48px" runat="server"
Width="272px" Height="16px" ForeColor="Red">I want to display notes
of selected employee</asp:Label>
<asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 72px"
runat="server" Width="704px" Height="56px"></asp:Label></form>
</body>
</HTML>

--------------------------------------------------code
behind--------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected DataSet ds1 = new DataSet();
protected System.Web.UI.WebControls.Label lblDisplay;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblSpecific;
protected System.Web.UI.WebControls.DropDownList ddLastName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string connectionString = "data source =localhost; initial catalog
=Northwind; integrated security=true;";
SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand sqlCommand1 = new SqlCommand();
SqlDataAdapter sda1 = new SqlDataAdapter();

// "dataset defined at class level"
// DataSet ds1 = new DataSet();

sda1.SelectCommand = sqlCommand1;
sda1.SelectCommand.Connection = sqlConnection1;
sda1.SelectCommand.CommandType = CommandType.Text;
sda1.SelectCommand.CommandText = "select * from Employees";

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

sda2.SelectCommand = sqlCommand1;
sda2.SelectCommand.Connection = sqlConnection1;
sda2.SelectCommand.CommandType = CommandType.Text;
sda2.SelectCommand.CommandText = "select * from Customers";

sda2.Fill(ds1,"Customers");

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

// to fill with specific cell data
lblSpecific.Text = "Displaying specific Data:<br><br>";
lblSpecific.Text +=
ds1.Tables["Employees"].Rows[0]["Notes"].ToString();

// to fill the datagrid
DataGrid1.DataSource = ds1;
DataGrid1.DataMember = "Employees";
DataGrid1.DataBind();

}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}



Nov 19 '05 #4
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your right
about the constructor but that was not meant to be tested code just really
to point you in the right direction. In this example, the form is simply a
main switchboard which does not use any data, on the other forms in the
application they define the dataset, and adapter as class level variables
and load them from cache in the Page_Load event as described which can be
done with a simple assignment variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things
to set and there is no overloading of Cache.Add() that takes 1 parameter
as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

Once a Webform is constructed and the page rendered, the object is
destroyed. You need to arrange for the data to be filled on the initial
get and saved to the cache for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

I have a label called lblDisplay

I want the label to display the Notes column data of the selected item
in the dropdownList.

My code is causing a NullReferenceException.
Can anyone find out what is the problem please?

------------------------------------
Code: ----------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm9</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
POSITION: absolute; TOP: 19px"
runat="server" Width="536px" Height="8px"></asp:Label>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
POSITION: absolute; TOP: 152px"
runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
BackColor="#003399"></HeaderStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
absolute; TOP: 48px" runat="server"
Width="272px" Height="16px" ForeColor="Red">I want to display notes
of selected employee</asp:Label>
<asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
POSITION: absolute; TOP: 72px"
runat="server" Width="704px" Height="56px"></asp:Label></form>
</body>
</HTML>

--------------------------------------------------code
behind--------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm9.
/// </summary>
public class WebForm9 : System.Web.UI.Page
{
protected DataSet ds1 = new DataSet();
protected System.Web.UI.WebControls.Label lblDisplay;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblSpecific;
protected System.Web.UI.WebControls.DropDownList ddLastName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string connectionString = "data source =localhost; initial catalog
=Northwind; integrated security=true;";
SqlConnection sqlConnection1 = new SqlConnection(connectionString);

SqlCommand sqlCommand1 = new SqlCommand();
SqlDataAdapter sda1 = new SqlDataAdapter();

// "dataset defined at class level"
// DataSet ds1 = new DataSet();

sda1.SelectCommand = sqlCommand1;
sda1.SelectCommand.Connection = sqlConnection1;
sda1.SelectCommand.CommandType = CommandType.Text;
sda1.SelectCommand.CommandText = "select * from Employees";

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

sda2.SelectCommand = sqlCommand1;
sda2.SelectCommand.Connection = sqlConnection1;
sda2.SelectCommand.CommandType = CommandType.Text;
sda2.SelectCommand.CommandText = "select * from Customers";

sda2.Fill(ds1,"Customers");

ddLastName.DataSource = ds1;
ddLastName.DataMember = "Employees";
ddLastName.DataTextField = "LastName";
ddLastName.DataBind();
ddLastName.Items.Insert(0,"Select:");

// to fill with specific cell data
lblSpecific.Text = "Displaying specific Data:<br><br>";
lblSpecific.Text +=
ds1.Tables["Employees"].Rows[0]["Notes"].ToString();

// to fill the datagrid
DataGrid1.DataSource = ds1;
DataGrid1.DataMember = "Employees";
DataGrid1.DataBind();

}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}



Nov 19 '05 #5
I could, but you hould do some research yourself. This is not rocket science
(only a few simple lines) and there are translators out on the web which
will do this for you if you look.

I don't mean to be tough on you, but you do need to get hold of the
pioneering spirit in order to progress. Twenty years ago, I was working in
an electronics lab fixing some equipment and I was having trouble with one
particular circuit. I asked my boss for help, and his reply was to slap down
hard the manual on my desk and he told me to read it. I was furious at him
and decided never to ask him for help again and then subsequently became an
expert on this particular equipment.

I think his approach was perhaps a bit draconian, however, it made me more
self sufficient than I perhaps would have been had everything been done for
me.

Regards - Mr N . . . .


"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your right
about the constructor but that was not meant to be tested code just
really to point you in the right direction. In this example, the form is
simply a main switchboard which does not use any data, on the other forms
in the application they define the dataset, and adapter as class level
variables and load them from cache in the Page_Load event as described
which can be done with a simple assignment
variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult
things to set and there is no overloading of Cache.Add() that takes 1
parameter as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

Once a Webform is constructed and the page rendered, the object is
destroyed. You need to arrange for the data to be filled on the initial
get and saved to the cache for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
>I have a dataset called ds1 filled with 2 tables Employees and
>Customers from Northwind database.
> I have dropdownList called ddLastName with the following properties:
>
> ddLastName.DataSource = ds1;
> ddLastName.DataMember = "Employees";
> ddLastName.DataTextField = "LastName";
> ddLastName.DataBind();
> ddLastName.Items.Insert(0,"Select:");
>
> I have a label called lblDisplay
>
> I want the label to display the Notes column data of the selected item
> in the dropdownList.
>
> My code is causing a NullReferenceException.
> Can anyone find out what is the problem please?
>
> ------------------------------------
> Code: ----------------------------------------------------
> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
> AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
> <HEAD>
> <title>WebForm9</title>
> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
> <meta content="C#" name="CODE_LANGUAGE">
> <meta content="JavaScript" name="vs_defaultClientScript">
> <meta content="http://schemas.microsoft.com/intellisense/ie5"
> name="vs_targetSchema">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
> POSITION: absolute; TOP: 19px"
> runat="server" Width="536px" Height="8px"></asp:Label>
> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
> POSITION: absolute; TOP: 152px"
> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
> BorderStyle="None" BorderWidth="1px"
> BackColor="White" CellPadding="4">
> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
> BackColor="#009999"></SelectedItemStyle>
> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
> BackColor="#003399"></HeaderStyle>
> <PagerStyle HorizontalAlign="Left" ForeColor="#003399"
> BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
> POSITION: absolute; TOP: 16px"
> runat="server" Width="112px" Height="24px"
> AutoPostBack="True"></asp:DropDownList>
> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
> absolute; TOP: 48px" runat="server"
> Width="272px" Height="16px" ForeColor="Red">I want to display notes
> of selected employee</asp:Label>
> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
> POSITION: absolute; TOP: 72px"
> runat="server" Width="704px" Height="56px"></asp:Label></form>
> </body>
> </HTML>
>
> --------------------------------------------------code
> behind--------------------------------------------------------------------------
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Data.SqlClient;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
>
> namespace WebApplication2
> {
> /// <summary>
> /// Summary description for WebForm9.
> /// </summary>
> public class WebForm9 : System.Web.UI.Page
> {
> protected DataSet ds1 = new DataSet();
> protected System.Web.UI.WebControls.Label lblDisplay;
> protected System.Web.UI.WebControls.DataGrid DataGrid1;
> protected System.Web.UI.WebControls.Label lblText;
> protected System.Web.UI.WebControls.Label lblSpecific;
> protected System.Web.UI.WebControls.DropDownList ddLastName;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if(!Page.IsPostBack)
> {
> string connectionString = "data source =localhost; initial catalog
> =Northwind; integrated security=true;";
> SqlConnection sqlConnection1 = new SqlConnection(connectionString);
>
> SqlCommand sqlCommand1 = new SqlCommand();
> SqlDataAdapter sda1 = new SqlDataAdapter();
>
> // "dataset defined at class level"
> // DataSet ds1 = new DataSet();
>
> sda1.SelectCommand = sqlCommand1;
> sda1.SelectCommand.Connection = sqlConnection1;
> sda1.SelectCommand.CommandType = CommandType.Text;
> sda1.SelectCommand.CommandText = "select * from Employees";
>
> sda1.Fill(ds1,"Employees");
>
> SqlDataAdapter sda2 = new SqlDataAdapter();
>
> sda2.SelectCommand = sqlCommand1;
> sda2.SelectCommand.Connection = sqlConnection1;
> sda2.SelectCommand.CommandType = CommandType.Text;
> sda2.SelectCommand.CommandText = "select * from Customers";
>
> sda2.Fill(ds1,"Customers");
>
> ddLastName.DataSource = ds1;
> ddLastName.DataMember = "Employees";
> ddLastName.DataTextField = "LastName";
> ddLastName.DataBind();
> ddLastName.Items.Insert(0,"Select:");
>
> // to fill with specific cell data
> lblSpecific.Text = "Displaying specific Data:<br><br>";
> lblSpecific.Text +=
> ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>
> // to fill the datagrid
> DataGrid1.DataSource = ds1;
> DataGrid1.DataMember = "Employees";
> DataGrid1.DataBind();
>
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.ddLastName.SelectedIndexChanged += new
> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
> private void ddLastName_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> if(ddLastName.SelectedIndex != 0)
> {
> int i = 0;
> foreach(DataRow r in ds1.Tables["Employees"].Rows)
> {
> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
> {
> lblDisplay.Text =
> ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
> }
> i++;
> }
> }
> else
> {
> lblDisplay.Text = "";
> }
> }
> }
> }
>



Nov 19 '05 #6
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C# because I don't
understand vb.net.
Many Thanks. "Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your right about the
constructor but that was not meant to be tested code just really to point you in the
right direction. In this example, the form is simply a main switchboard which does not
use any data, on the other forms in the application they define the dataset, and
adapter as class level variables and load them from cache in the Page_Load event as
described which can be done with a simple assignment
variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things to set and
there is no overloading of Cache.Add() that takes 1 parameter as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
The problem is that when you click the Dropdown Box, you trigger a postback.
Unfortunately, your Page_Load event only fills the dataset during the initial GET. So
there is no data to be had when you do your Autopostback.

Once a Webform is constructed and the page rendered, the object is destroyed. You
need to arrange for the data to be filled on the initial get and saved to the cache
for example.

cache("ds1)= Ds1
and as an else condition to your if not IsPostback

ds1 = cache("ds1")

page.DataBind() .. . . . .etc

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
>I have a dataset called ds1 filled with 2 tables Employees and Customers from
>Northwind database.
> I have dropdownList called ddLastName with the following properties:
>
> ddLastName.DataSource = ds1;
> ddLastName.DataMember = "Employees";
> ddLastName.DataTextField = "LastName";
> ddLastName.DataBind();
> ddLastName.Items.Insert(0,"Select:");
>
> I have a label called lblDisplay
>
> I want the label to display the Notes column data of the selected item in the
> dropdownList.
>
> My code is causing a NullReferenceException.
> Can anyone find out what is the problem please?
>
> ------------------------------------
> Code: ----------------------------------------------------
> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false"
> Inherits="WebApplication2.WebForm9" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
> <HEAD>
> <title>WebForm9</title>
> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
> <meta content="C#" name="CODE_LANGUAGE">
> <meta content="JavaScript" name="vs_defaultClientScript">
> <meta content="http://schemas.microsoft.com/intellisense/ie5"
> name="vs_targetSchema">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute;
> TOP: 19px"
> runat="server" Width="536px" Height="8px"></asp:Label>
> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute;
> TOP: 152px"
> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
> BorderStyle="None" BorderWidth="1px"
> BackColor="White" CellPadding="4">
> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
> BackColor="#009999"></SelectedItemStyle>
> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
> BackColor="#003399"></HeaderStyle>
> <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC"
> Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px; POSITION:
> absolute; TOP: 16px"
> runat="server" Width="112px" Height="24px"
> AutoPostBack="True"></asp:DropDownList>
> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute; TOP:
> 48px" runat="server"
> Width="272px" Height="16px" ForeColor="Red">I want to display notes of selected
> employee</asp:Label>
> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute;
> TOP: 72px"
> runat="server" Width="704px" Height="56px"></asp:Label></form>
> </body>
> </HTML>
>
> --------------------------------------------------code
> behind--------------------------------------------------------------------------
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Data.SqlClient;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
>
> namespace WebApplication2
> {
> /// <summary>
> /// Summary description for WebForm9.
> /// </summary>
> public class WebForm9 : System.Web.UI.Page
> {
> protected DataSet ds1 = new DataSet();
> protected System.Web.UI.WebControls.Label lblDisplay;
> protected System.Web.UI.WebControls.DataGrid DataGrid1;
> protected System.Web.UI.WebControls.Label lblText;
> protected System.Web.UI.WebControls.Label lblSpecific;
> protected System.Web.UI.WebControls.DropDownList ddLastName;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if(!Page.IsPostBack)
> {
> string connectionString = "data source =localhost; initial catalog =Northwind;
> integrated security=true;";
> SqlConnection sqlConnection1 = new SqlConnection(connectionString);
>
> SqlCommand sqlCommand1 = new SqlCommand();
> SqlDataAdapter sda1 = new SqlDataAdapter();
>
> // "dataset defined at class level"
> // DataSet ds1 = new DataSet();
>
> sda1.SelectCommand = sqlCommand1;
> sda1.SelectCommand.Connection = sqlConnection1;
> sda1.SelectCommand.CommandType = CommandType.Text;
> sda1.SelectCommand.CommandText = "select * from Employees";
>
> sda1.Fill(ds1,"Employees");
>
> SqlDataAdapter sda2 = new SqlDataAdapter();
>
> sda2.SelectCommand = sqlCommand1;
> sda2.SelectCommand.Connection = sqlConnection1;
> sda2.SelectCommand.CommandType = CommandType.Text;
> sda2.SelectCommand.CommandText = "select * from Customers";
>
> sda2.Fill(ds1,"Customers");
>
> ddLastName.DataSource = ds1;
> ddLastName.DataMember = "Employees";
> ddLastName.DataTextField = "LastName";
> ddLastName.DataBind();
> ddLastName.Items.Insert(0,"Select:");
>
> // to fill with specific cell data
> lblSpecific.Text = "Displaying specific Data:<br><br>";
> lblSpecific.Text += ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>
> // to fill the datagrid
> DataGrid1.DataSource = ds1;
> DataGrid1.DataMember = "Employees";
> DataGrid1.DataBind();
>
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.ddLastName.SelectedIndexChanged += new
> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
> private void ddLastName_SelectedIndexChanged(object sender, System.EventArgs e)
> {
> if(ddLastName.SelectedIndex != 0)
> {
> int i = 0;
> foreach(DataRow r in ds1.Tables["Employees"].Rows)
> {
> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
> {
> lblDisplay.Text = ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
> }
> i++;
> }
> }
> else
> {
> lblDisplay.Text = "";
> }
> }
> }
> }
>



Nov 19 '05 #7
Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still,
thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your right
about the constructor but that was not meant to be tested code just
really to point you in the right direction. In this example, the form is
simply a main switchboard which does not use any data, on the other
forms in the application they define the dataset, and adapter as class
level variables and load them from cache in the Page_Load event as
described which can be done with a simple assignment
variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default,
Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult
things to set and there is no overloading of Cache.Add() that takes 1
parameter as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
> The problem is that when you click the Dropdown Box, you trigger a
> postback. Unfortunately, your Page_Load event only fills the dataset
> during the initial GET. So there is no data to be had when you do your
> Autopostback.
>
> Once a Webform is constructed and the page rendered, the object is
> destroyed. You need to arrange for the data to be filled on the
> initial get and saved to the cache for example.
>
> cache("ds1)= Ds1
>
>
> and as an else condition to your if not IsPostback
>
> ds1 = cache("ds1")
>
> page.DataBind() .. . . . .etc
>
> PS: This is a common mistake to make.
>
> HTH
>
>
>
> "Bishoy George" <bi****@bishoy.com> wrote in message
> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>I have a dataset called ds1 filled with 2 tables Employees and
>>Customers from Northwind database.
>> I have dropdownList called ddLastName with the following properties:
>>
>> ddLastName.DataSource = ds1;
>> ddLastName.DataMember = "Employees";
>> ddLastName.DataTextField = "LastName";
>> ddLastName.DataBind();
>> ddLastName.Items.Insert(0,"Select:");
>>
>> I have a label called lblDisplay
>>
>> I want the label to display the Notes column data of the selected
>> item in the dropdownList.
>>
>> My code is causing a NullReferenceException.
>> Can anyone find out what is the problem please?
>>
>> ------------------------------------
>> Code: ----------------------------------------------------
>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
>> AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>> <HTML>
>> <HEAD>
>> <title>WebForm9</title>
>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>> <meta content="C#" name="CODE_LANGUAGE">
>> <meta content="JavaScript" name="vs_defaultClientScript">
>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>> name="vs_targetSchema">
>> </HEAD>
>> <body MS_POSITIONING="GridLayout">
>> <form id="Form1" method="post" runat="server">
>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
>> POSITION: absolute; TOP: 19px"
>> runat="server" Width="536px" Height="8px"></asp:Label>
>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
>> POSITION: absolute; TOP: 152px"
>> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
>> BorderStyle="None" BorderWidth="1px"
>> BackColor="White" CellPadding="4">
>> <FooterStyle ForeColor="#003399"
>> BackColor="#99CCCC"></FooterStyle>
>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>> BackColor="#009999"></SelectedItemStyle>
>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>> BackColor="#003399"></HeaderStyle>
>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399"
>> BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
>> </asp:DataGrid>
>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
>> POSITION: absolute; TOP: 16px"
>> runat="server" Width="112px" Height="24px"
>> AutoPostBack="True"></asp:DropDownList>
>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION:
>> absolute; TOP: 48px" runat="server"
>> Width="272px" Height="16px" ForeColor="Red">I want to display
>> notes of selected employee</asp:Label>
>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
>> POSITION: absolute; TOP: 72px"
>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>> </body>
>> </HTML>
>>
>> --------------------------------------------------code
>> behind--------------------------------------------------------------------------
>> using System;
>> using System.Collections;
>> using System.ComponentModel;
>> using System.Data;
>> using System.Data.SqlClient;
>> using System.Drawing;
>> using System.Web;
>> using System.Web.SessionState;
>> using System.Web.UI;
>> using System.Web.UI.WebControls;
>> using System.Web.UI.HtmlControls;
>>
>> namespace WebApplication2
>> {
>> /// <summary>
>> /// Summary description for WebForm9.
>> /// </summary>
>> public class WebForm9 : System.Web.UI.Page
>> {
>> protected DataSet ds1 = new DataSet();
>> protected System.Web.UI.WebControls.Label lblDisplay;
>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>> protected System.Web.UI.WebControls.Label lblText;
>> protected System.Web.UI.WebControls.Label lblSpecific;
>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>
>> private void Page_Load(object sender, System.EventArgs e)
>> {
>> if(!Page.IsPostBack)
>> {
>> string connectionString = "data source =localhost; initial catalog
>> =Northwind; integrated security=true;";
>> SqlConnection sqlConnection1 = new
>> SqlConnection(connectionString);
>>
>> SqlCommand sqlCommand1 = new SqlCommand();
>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>
>> // "dataset defined at class level"
>> // DataSet ds1 = new DataSet();
>>
>> sda1.SelectCommand = sqlCommand1;
>> sda1.SelectCommand.Connection = sqlConnection1;
>> sda1.SelectCommand.CommandType = CommandType.Text;
>> sda1.SelectCommand.CommandText = "select * from Employees";
>>
>> sda1.Fill(ds1,"Employees");
>>
>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>
>> sda2.SelectCommand = sqlCommand1;
>> sda2.SelectCommand.Connection = sqlConnection1;
>> sda2.SelectCommand.CommandType = CommandType.Text;
>> sda2.SelectCommand.CommandText = "select * from Customers";
>>
>> sda2.Fill(ds1,"Customers");
>>
>> ddLastName.DataSource = ds1;
>> ddLastName.DataMember = "Employees";
>> ddLastName.DataTextField = "LastName";
>> ddLastName.DataBind();
>> ddLastName.Items.Insert(0,"Select:");
>>
>> // to fill with specific cell data
>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>> lblSpecific.Text +=
>> ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>
>> // to fill the datagrid
>> DataGrid1.DataSource = ds1;
>> DataGrid1.DataMember = "Employees";
>> DataGrid1.DataBind();
>>
>> }
>> }
>>
>> #region Web Form Designer generated code
>> override protected void OnInit(EventArgs e)
>> {
>> //
>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
>> //
>> InitializeComponent();
>> base.OnInit(e);
>> }
>>
>> /// <summary>
>> /// Required method for Designer support - do not modify
>> /// the contents of this method with the code editor.
>> /// </summary>
>> private void InitializeComponent()
>> {
>> this.ddLastName.SelectedIndexChanged += new
>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>> this.Load += new System.EventHandler(this.Page_Load);
>>
>> }
>> #endregion
>>
>> private void ddLastName_SelectedIndexChanged(object sender,
>> System.EventArgs e)
>> {
>> if(ddLastName.SelectedIndex != 0)
>> {
>> int i = 0;
>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>> {
>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>> {
>> lblDisplay.Text =
>> ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>> }
>> i++;
>> }
>> }
>> else
>> {
>> lblDisplay.Text = "";
>> }
>> }
>> }
>> }
>>
>
>



Nov 19 '05 #8
re:
Many thanks Juan for this utility for conversion.
You're very welcome.

re: Mr.Newbie:
your code has errors
Actually, it doesn't.

You have to work around the limitations of the conversion tool,
by making sure you insert code-line breaks ( the underscore in VB.NET )
so that the conversion utility doesn't think the code statements are truncated.

So, if you insert underscores at the end of the longest lines,
the conversion tool can understand them.

For example, here's an appropiately marked source segment :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)

End If
End Sub

That is correctly converted by the conversion utility to :

private void Page_Load(object sender, System.EventArgs e)
{
if (!(Page.IsPostBack)) {
this.adaptCalls.Fill(this.DsCalls1);
this.adaptContacts.Fill(this.DsContacts1);
this.adaptContactTypes.Fill(this.DsContactTypes1);
AddToCache("adaptCalls", adaptCalls);
AddToCache("adaptContacts", adaptContacts);
AddToCache("adaptContactTypes", adaptContactTypes);
AddToCache("dsCalls", DsCalls1);
AddToCache("dsContacts", DsContacts1);
AddToCache("dsContactTypes", DsContactTypes1);
}
}

btw, the conversion utility may not do a complete conversion,
or even a completely accurate one, but it sure saves a lot of
typing, since -mostly- checking code for errors is much easier
than writing it.

I'm sure some will disagree on this... ;-)


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl... Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still, thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C# because I don't
understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your right about the
constructor but that was not meant to be tested code just really to point you in the
right direction. In this example, the form is simply a main switchboard which does
not use any data, on the other forms in the application they define the dataset, and
adapter as class level variables and load them from cache in the Page_Load event as
described which can be done with a simple assignment
variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
> You are right. But, please write me more specific coding about caching:
> 1- code of caching exactly: as I looked at msdn there is difficult things to set and
> there is no overloading of Cache.Add() that takes 1 parameter as you wrote.
> 2- where exactly to put that caching code?
> Thanks.
>
> "Mr Newbie" <he**@now.com> wrote in message
> news:uV**************@tk2msftngp13.phx.gbl...
>> The problem is that when you click the Dropdown Box, you trigger a postback.
>> Unfortunately, your Page_Load event only fills the dataset during the initial GET.
>> So there is no data to be had when you do your Autopostback.
>>
>> Once a Webform is constructed and the page rendered, the object is destroyed. You
>> need to arrange for the data to be filled on the initial get and saved to the cache
>> for example.
>>
>> cache("ds1)= Ds1
>>
>>
>> and as an else condition to your if not IsPostback
>>
>> ds1 = cache("ds1")
>>
>> page.DataBind() .. . . . .etc
>>
>> PS: This is a common mistake to make.
>>
>> HTH
>>
>>
>>
>> "Bishoy George" <bi****@bishoy.com> wrote in message
>> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>>I have a dataset called ds1 filled with 2 tables Employees and Customers from
>>>Northwind database.
>>> I have dropdownList called ddLastName with the following properties:
>>>
>>> ddLastName.DataSource = ds1;
>>> ddLastName.DataMember = "Employees";
>>> ddLastName.DataTextField = "LastName";
>>> ddLastName.DataBind();
>>> ddLastName.Items.Insert(0,"Select:");
>>>
>>> I have a label called lblDisplay
>>>
>>> I want the label to display the Notes column data of the selected item in the
>>> dropdownList.
>>>
>>> My code is causing a NullReferenceException.
>>> Can anyone find out what is the problem please?
>>>
>>> ------------------------------------
>>> Code: ----------------------------------------------------
>>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false"
>>> Inherits="WebApplication2.WebForm9" %>
>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>>> <HTML>
>>> <HEAD>
>>> <title>WebForm9</title>
>>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>>> <meta content="C#" name="CODE_LANGUAGE">
>>> <meta content="JavaScript" name="vs_defaultClientScript">
>>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>>> name="vs_targetSchema">
>>> </HEAD>
>>> <body MS_POSITIONING="GridLayout">
>>> <form id="Form1" method="post" runat="server">
>>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute;
>>> TOP: 19px"
>>> runat="server" Width="536px" Height="8px"></asp:Label>
>>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION:
>>> absolute; TOP: 152px"
>>> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
>>> BorderStyle="None" BorderWidth="1px"
>>> BackColor="White" CellPadding="4">
>>> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
>>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>>> BackColor="#009999"></SelectedItemStyle>
>>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>>> BackColor="#003399"></HeaderStyle>
>>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC"
>>> Mode="NumericPages"></PagerStyle>
>>> </asp:DataGrid>
>>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px; POSITION:
>>> absolute; TOP: 16px"
>>> runat="server" Width="112px" Height="24px"
>>> AutoPostBack="True"></asp:DropDownList>
>>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute;
>>> TOP: 48px" runat="server"
>>> Width="272px" Height="16px" ForeColor="Red">I want to display notes of selected
>>> employee</asp:Label>
>>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute;
>>> TOP: 72px"
>>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>>> </body>
>>> </HTML>
>>>
>>> --------------------------------------------------code
>>> behind--------------------------------------------------------------------------
>>> using System;
>>> using System.Collections;
>>> using System.ComponentModel;
>>> using System.Data;
>>> using System.Data.SqlClient;
>>> using System.Drawing;
>>> using System.Web;
>>> using System.Web.SessionState;
>>> using System.Web.UI;
>>> using System.Web.UI.WebControls;
>>> using System.Web.UI.HtmlControls;
>>>
>>> namespace WebApplication2
>>> {
>>> /// <summary>
>>> /// Summary description for WebForm9.
>>> /// </summary>
>>> public class WebForm9 : System.Web.UI.Page
>>> {
>>> protected DataSet ds1 = new DataSet();
>>> protected System.Web.UI.WebControls.Label lblDisplay;
>>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>>> protected System.Web.UI.WebControls.Label lblText;
>>> protected System.Web.UI.WebControls.Label lblSpecific;
>>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>>
>>> private void Page_Load(object sender, System.EventArgs e)
>>> {
>>> if(!Page.IsPostBack)
>>> {
>>> string connectionString = "data source =localhost; initial catalog =Northwind;
>>> integrated security=true;";
>>> SqlConnection sqlConnection1 = new SqlConnection(connectionString);
>>>
>>> SqlCommand sqlCommand1 = new SqlCommand();
>>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>>
>>> // "dataset defined at class level"
>>> // DataSet ds1 = new DataSet();
>>>
>>> sda1.SelectCommand = sqlCommand1;
>>> sda1.SelectCommand.Connection = sqlConnection1;
>>> sda1.SelectCommand.CommandType = CommandType.Text;
>>> sda1.SelectCommand.CommandText = "select * from Employees";
>>>
>>> sda1.Fill(ds1,"Employees");
>>>
>>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>>
>>> sda2.SelectCommand = sqlCommand1;
>>> sda2.SelectCommand.Connection = sqlConnection1;
>>> sda2.SelectCommand.CommandType = CommandType.Text;
>>> sda2.SelectCommand.CommandText = "select * from Customers";
>>>
>>> sda2.Fill(ds1,"Customers");
>>>
>>> ddLastName.DataSource = ds1;
>>> ddLastName.DataMember = "Employees";
>>> ddLastName.DataTextField = "LastName";
>>> ddLastName.DataBind();
>>> ddLastName.Items.Insert(0,"Select:");
>>>
>>> // to fill with specific cell data
>>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>>> lblSpecific.Text += ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>>
>>> // to fill the datagrid
>>> DataGrid1.DataSource = ds1;
>>> DataGrid1.DataMember = "Employees";
>>> DataGrid1.DataBind();
>>>
>>> }
>>> }
>>>
>>> #region Web Form Designer generated code
>>> override protected void OnInit(EventArgs e)
>>> {
>>> //
>>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
>>> //
>>> InitializeComponent();
>>> base.OnInit(e);
>>> }
>>>
>>> /// <summary>
>>> /// Required method for Designer support - do not modify
>>> /// the contents of this method with the code editor.
>>> /// </summary>
>>> private void InitializeComponent()
>>> {
>>> this.ddLastName.SelectedIndexChanged += new
>>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>>> this.Load += new System.EventHandler(this.Page_Load);
>>>
>>> }
>>> #endregion
>>>
>>> private void ddLastName_SelectedIndexChanged(object sender, System.EventArgs e)
>>> {
>>> if(ddLastName.SelectedIndex != 0)
>>> {
>>> int i = 0;
>>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>>> {
>>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>>> {
>>> lblDisplay.Text = ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>>> }
>>> i++;
>>> }
>>> }
>>> else
>>> {
>>> lblDisplay.Text = "";
>>> }
>>> }
>>> }
>>> }
>>>
>>
>>
>
>




Nov 19 '05 #9
My code does NOT have errors. This is from a working project.

"Bishoy George" <bi****@bishoy.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still,
thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
Here's a little example which uses a helper function. And yes your
right about the constructor but that was not meant to be tested code
just really to point you in the right direction. In this example, the
form is simply a main switchboard which does not use any data, on the
other forms in the application they define the dataset, and adapter as
class level variables and load them from cache in the Page_Load event
as described which can be done with a simple assignment
variableName=cache("storedIdentifier").

Hope this helps you.

Regards - Mr N. . . .

----------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)
End If
End Sub

Private Sub AddToCache(ByVal name As String, ByVal item As Object)
If Not IsNothing(Cache(name)) Then
Return
Else
Cache.Add(name, item, Nothing, DateTime.MaxValue,
System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default,
Nothing)
End If
End Sub

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
> You are right. But, please write me more specific coding about
> caching:
> 1- code of caching exactly: as I looked at msdn there is difficult
> things to set and there is no overloading of Cache.Add() that takes 1
> parameter as you wrote.
> 2- where exactly to put that caching code?
> Thanks.
>
> "Mr Newbie" <he**@now.com> wrote in message
> news:uV**************@tk2msftngp13.phx.gbl...
>> The problem is that when you click the Dropdown Box, you trigger a
>> postback. Unfortunately, your Page_Load event only fills the dataset
>> during the initial GET. So there is no data to be had when you do
>> your Autopostback.
>>
>> Once a Webform is constructed and the page rendered, the object is
>> destroyed. You need to arrange for the data to be filled on the
>> initial get and saved to the cache for example.
>>
>> cache("ds1)= Ds1
>>
>>
>> and as an else condition to your if not IsPostback
>>
>> ds1 = cache("ds1")
>>
>> page.DataBind() .. . . . .etc
>>
>> PS: This is a common mistake to make.
>>
>> HTH
>>
>>
>>
>> "Bishoy George" <bi****@bishoy.com> wrote in message
>> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>>I have a dataset called ds1 filled with 2 tables Employees and
>>>Customers from Northwind database.
>>> I have dropdownList called ddLastName with the following properties:
>>>
>>> ddLastName.DataSource = ds1;
>>> ddLastName.DataMember = "Employees";
>>> ddLastName.DataTextField = "LastName";
>>> ddLastName.DataBind();
>>> ddLastName.Items.Insert(0,"Select:");
>>>
>>> I have a label called lblDisplay
>>>
>>> I want the label to display the Notes column data of the selected
>>> item in the dropdownList.
>>>
>>> My code is causing a NullReferenceException.
>>> Can anyone find out what is the problem please?
>>>
>>> ------------------------------------
>>> Code: ----------------------------------------------------
>>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
>>> AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>>> <HTML>
>>> <HEAD>
>>> <title>WebForm9</title>
>>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>>> <meta content="C#" name="CODE_LANGUAGE">
>>> <meta content="JavaScript" name="vs_defaultClientScript">
>>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>>> name="vs_targetSchema">
>>> </HEAD>
>>> <body MS_POSITIONING="GridLayout">
>>> <form id="Form1" method="post" runat="server">
>>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
>>> POSITION: absolute; TOP: 19px"
>>> runat="server" Width="536px" Height="8px"></asp:Label>
>>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
>>> POSITION: absolute; TOP: 152px"
>>> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
>>> BorderStyle="None" BorderWidth="1px"
>>> BackColor="White" CellPadding="4">
>>> <FooterStyle ForeColor="#003399"
>>> BackColor="#99CCCC"></FooterStyle>
>>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>>> BackColor="#009999"></SelectedItemStyle>
>>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>>> BackColor="#003399"></HeaderStyle>
>>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399"
>>> BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
>>> </asp:DataGrid>
>>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
>>> POSITION: absolute; TOP: 16px"
>>> runat="server" Width="112px" Height="24px"
>>> AutoPostBack="True"></asp:DropDownList>
>>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px;
>>> POSITION: absolute; TOP: 48px" runat="server"
>>> Width="272px" Height="16px" ForeColor="Red">I want to display
>>> notes of selected employee</asp:Label>
>>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
>>> POSITION: absolute; TOP: 72px"
>>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>>> </body>
>>> </HTML>
>>>
>>> --------------------------------------------------code
>>> behind--------------------------------------------------------------------------
>>> using System;
>>> using System.Collections;
>>> using System.ComponentModel;
>>> using System.Data;
>>> using System.Data.SqlClient;
>>> using System.Drawing;
>>> using System.Web;
>>> using System.Web.SessionState;
>>> using System.Web.UI;
>>> using System.Web.UI.WebControls;
>>> using System.Web.UI.HtmlControls;
>>>
>>> namespace WebApplication2
>>> {
>>> /// <summary>
>>> /// Summary description for WebForm9.
>>> /// </summary>
>>> public class WebForm9 : System.Web.UI.Page
>>> {
>>> protected DataSet ds1 = new DataSet();
>>> protected System.Web.UI.WebControls.Label lblDisplay;
>>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>>> protected System.Web.UI.WebControls.Label lblText;
>>> protected System.Web.UI.WebControls.Label lblSpecific;
>>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>>
>>> private void Page_Load(object sender, System.EventArgs e)
>>> {
>>> if(!Page.IsPostBack)
>>> {
>>> string connectionString = "data source =localhost; initial
>>> catalog =Northwind; integrated security=true;";
>>> SqlConnection sqlConnection1 = new
>>> SqlConnection(connectionString);
>>>
>>> SqlCommand sqlCommand1 = new SqlCommand();
>>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>>
>>> // "dataset defined at class level"
>>> // DataSet ds1 = new DataSet();
>>>
>>> sda1.SelectCommand = sqlCommand1;
>>> sda1.SelectCommand.Connection = sqlConnection1;
>>> sda1.SelectCommand.CommandType = CommandType.Text;
>>> sda1.SelectCommand.CommandText = "select * from Employees";
>>>
>>> sda1.Fill(ds1,"Employees");
>>>
>>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>>
>>> sda2.SelectCommand = sqlCommand1;
>>> sda2.SelectCommand.Connection = sqlConnection1;
>>> sda2.SelectCommand.CommandType = CommandType.Text;
>>> sda2.SelectCommand.CommandText = "select * from Customers";
>>>
>>> sda2.Fill(ds1,"Customers");
>>>
>>> ddLastName.DataSource = ds1;
>>> ddLastName.DataMember = "Employees";
>>> ddLastName.DataTextField = "LastName";
>>> ddLastName.DataBind();
>>> ddLastName.Items.Insert(0,"Select:");
>>>
>>> // to fill with specific cell data
>>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>>> lblSpecific.Text +=
>>> ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>>
>>> // to fill the datagrid
>>> DataGrid1.DataSource = ds1;
>>> DataGrid1.DataMember = "Employees";
>>> DataGrid1.DataBind();
>>>
>>> }
>>> }
>>>
>>> #region Web Form Designer generated code
>>> override protected void OnInit(EventArgs e)
>>> {
>>> //
>>> // CODEGEN: This call is required by the ASP.NET Web Form
>>> Designer.
>>> //
>>> InitializeComponent();
>>> base.OnInit(e);
>>> }
>>>
>>> /// <summary>
>>> /// Required method for Designer support - do not modify
>>> /// the contents of this method with the code editor.
>>> /// </summary>
>>> private void InitializeComponent()
>>> {
>>> this.ddLastName.SelectedIndexChanged += new
>>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>>> this.Load += new System.EventHandler(this.Page_Load);
>>>
>>> }
>>> #endregion
>>>
>>> private void ddLastName_SelectedIndexChanged(object sender,
>>> System.EventArgs e)
>>> {
>>> if(ddLastName.SelectedIndex != 0)
>>> {
>>> int i = 0;
>>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>>> {
>>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>>> {
>>> lblDisplay.Text =
>>> ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>>> }
>>> i++;
>>> }
>>> }
>>> else
>>> {
>>> lblDisplay.Text = "";
>>> }
>>> }
>>> }
>>> }
>>>
>>
>>
>
>



Nov 19 '05 #10
Thanks for the follow up on this Juan. I dont think I would have had the
energy

;-)

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uZ**************@TK2MSFTNGP15.phx.gbl...
re:
Many thanks Juan for this utility for conversion.


You're very welcome.

re:
Mr.Newbie:
your code has errors


Actually, it doesn't.

You have to work around the limitations of the conversion tool,
by making sure you insert code-line breaks ( the underscore in VB.NET )
so that the conversion utility doesn't think the code statements are
truncated.

So, if you insert underscores at the end of the longest lines,
the conversion tool can understand them.

For example, here's an appropiately marked source segment :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)

End If
End Sub

That is correctly converted by the conversion utility to :

private void Page_Load(object sender, System.EventArgs e)
{
if (!(Page.IsPostBack)) {
this.adaptCalls.Fill(this.DsCalls1);
this.adaptContacts.Fill(this.DsContacts1);
this.adaptContactTypes.Fill(this.DsContactTypes1);
AddToCache("adaptCalls", adaptCalls);
AddToCache("adaptContacts", adaptContacts);
AddToCache("adaptContactTypes", adaptContactTypes);
AddToCache("dsCalls", DsCalls1);
AddToCache("dsContacts", DsContacts1);
AddToCache("dsContactTypes", DsContactTypes1);
}
}

btw, the conversion utility may not do a complete conversion,
or even a completely accurate one, but it sure saves a lot of
typing, since -mostly- checking code for errors is much easier
than writing it.

I'm sure some will disagree on this... ;-)


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still,
thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C#
because I don't
understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
> Here's a little example which uses a helper function. And yes your
> right about the
> constructor but that was not meant to be tested code just really to
> point you in the
> right direction. In this example, the form is simply a main
> switchboard which does
> not use any data, on the other forms in the application they define
> the dataset, and
> adapter as class level variables and load them from cache in the
> Page_Load event as
> described which can be done with a simple assignment
> variableName=cache("storedIdentifier").
>
> Hope this helps you.
>
> Regards - Mr N. . . .
>
> ----------------------
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> Handles MyBase.Load
> 'Put user code to initialize the page here
>
> If Not Page.IsPostBack Then
>
> Me.adaptCalls.Fill(Me.DsCalls1)
> Me.adaptContacts.Fill(Me.DsContacts1)
> Me.adaptContactTypes.Fill(Me.DsContactTypes1)
>
> AddToCache("adaptCalls", adaptCalls)
> AddToCache("adaptContacts", adaptContacts)
> AddToCache("adaptContactTypes", adaptContactTypes)
>
> AddToCache("dsCalls", DsCalls1)
> AddToCache("dsContacts", DsContacts1)
> AddToCache("dsContactTypes", DsContactTypes1)
>
>
> End If
>
>
> End Sub
>
> Private Sub AddToCache(ByVal name As String, ByVal item As Object)
> If Not IsNothing(Cache(name)) Then
> Return
> Else
> Cache.Add(name, item, Nothing, DateTime.MaxValue,
> System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default,
> Nothing)
> End If
> End Sub
>
>
>
> "Bishoy George" <bi****@bishoy.com> wrote in message
> news:ul**************@TK2MSFTNGP11.phx.gbl...
>> You are right. But, please write me more specific coding about
>> caching:
>> 1- code of caching exactly: as I looked at msdn there is difficult
>> things to set and
>> there is no overloading of Cache.Add() that takes 1 parameter as you
>> wrote.
>> 2- where exactly to put that caching code?
>> Thanks.
>>
>> "Mr Newbie" <he**@now.com> wrote in message
>> news:uV**************@tk2msftngp13.phx.gbl...
>>> The problem is that when you click the Dropdown Box, you trigger a
>>> postback.
>>> Unfortunately, your Page_Load event only fills the dataset during
>>> the initial GET.
>>> So there is no data to be had when you do your Autopostback.
>>>
>>> Once a Webform is constructed and the page rendered, the object is
>>> destroyed. You
>>> need to arrange for the data to be filled on the initial get and
>>> saved to the cache
>>> for example.
>>>
>>> cache("ds1)= Ds1
>>>
>>>
>>> and as an else condition to your if not IsPostback
>>>
>>> ds1 = cache("ds1")
>>>
>>> page.DataBind() .. . . . .etc
>>>
>>> PS: This is a common mistake to make.
>>>
>>> HTH
>>>
>>>
>>>
>>> "Bishoy George" <bi****@bishoy.com> wrote in message
>>> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>>>I have a dataset called ds1 filled with 2 tables Employees and
>>>>Customers from
>>>>Northwind database.
>>>> I have dropdownList called ddLastName with the following
>>>> properties:
>>>>
>>>> ddLastName.DataSource = ds1;
>>>> ddLastName.DataMember = "Employees";
>>>> ddLastName.DataTextField = "LastName";
>>>> ddLastName.DataBind();
>>>> ddLastName.Items.Insert(0,"Select:");
>>>>
>>>> I have a label called lblDisplay
>>>>
>>>> I want the label to display the Notes column data of the selected
>>>> item in the
>>>> dropdownList.
>>>>
>>>> My code is causing a NullReferenceException.
>>>> Can anyone find out what is the problem please?
>>>>
>>>> ------------------------------------
>>>> Code: ----------------------------------------------------
>>>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
>>>> AutoEventWireup="false"
>>>> Inherits="WebApplication2.WebForm9" %>
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>>>> <HTML>
>>>> <HEAD>
>>>> <title>WebForm9</title>
>>>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>>>> <meta content="C#" name="CODE_LANGUAGE">
>>>> <meta content="JavaScript" name="vs_defaultClientScript">
>>>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>>>> name="vs_targetSchema">
>>>> </HEAD>
>>>> <body MS_POSITIONING="GridLayout">
>>>> <form id="Form1" method="post" runat="server">
>>>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
>>>> POSITION: absolute;
>>>> TOP: 19px"
>>>> runat="server" Width="536px" Height="8px"></asp:Label>
>>>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
>>>> POSITION:
>>>> absolute; TOP: 152px"
>>>> runat="server" Width="672px" Height="120px"
>>>> BorderColor="#3366CC"
>>>> BorderStyle="None" BorderWidth="1px"
>>>> BackColor="White" CellPadding="4">
>>>> <FooterStyle ForeColor="#003399"
>>>> BackColor="#99CCCC"></FooterStyle>
>>>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>>>> BackColor="#009999"></SelectedItemStyle>
>>>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>>>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>>>> BackColor="#003399"></HeaderStyle>
>>>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399"
>>>> BackColor="#99CCCC"
>>>> Mode="NumericPages"></PagerStyle>
>>>> </asp:DataGrid>
>>>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT:
>>>> 16px; POSITION:
>>>> absolute; TOP: 16px"
>>>> runat="server" Width="112px" Height="24px"
>>>> AutoPostBack="True"></asp:DropDownList>
>>>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px;
>>>> POSITION: absolute;
>>>> TOP: 48px" runat="server"
>>>> Width="272px" Height="16px" ForeColor="Red">I want to display
>>>> notes of selected
>>>> employee</asp:Label>
>>>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
>>>> POSITION: absolute;
>>>> TOP: 72px"
>>>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>>>> </body>
>>>> </HTML>
>>>>
>>>> --------------------------------------------------code
>>>> behind--------------------------------------------------------------------------
>>>> using System;
>>>> using System.Collections;
>>>> using System.ComponentModel;
>>>> using System.Data;
>>>> using System.Data.SqlClient;
>>>> using System.Drawing;
>>>> using System.Web;
>>>> using System.Web.SessionState;
>>>> using System.Web.UI;
>>>> using System.Web.UI.WebControls;
>>>> using System.Web.UI.HtmlControls;
>>>>
>>>> namespace WebApplication2
>>>> {
>>>> /// <summary>
>>>> /// Summary description for WebForm9.
>>>> /// </summary>
>>>> public class WebForm9 : System.Web.UI.Page
>>>> {
>>>> protected DataSet ds1 = new DataSet();
>>>> protected System.Web.UI.WebControls.Label lblDisplay;
>>>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>>>> protected System.Web.UI.WebControls.Label lblText;
>>>> protected System.Web.UI.WebControls.Label lblSpecific;
>>>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>>>
>>>> private void Page_Load(object sender, System.EventArgs e)
>>>> {
>>>> if(!Page.IsPostBack)
>>>> {
>>>> string connectionString = "data source =localhost; initial
>>>> catalog =Northwind;
>>>> integrated security=true;";
>>>> SqlConnection sqlConnection1 = new
>>>> SqlConnection(connectionString);
>>>>
>>>> SqlCommand sqlCommand1 = new SqlCommand();
>>>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>>>
>>>> // "dataset defined at class level"
>>>> // DataSet ds1 = new DataSet();
>>>>
>>>> sda1.SelectCommand = sqlCommand1;
>>>> sda1.SelectCommand.Connection = sqlConnection1;
>>>> sda1.SelectCommand.CommandType = CommandType.Text;
>>>> sda1.SelectCommand.CommandText = "select * from Employees";
>>>>
>>>> sda1.Fill(ds1,"Employees");
>>>>
>>>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>>>
>>>> sda2.SelectCommand = sqlCommand1;
>>>> sda2.SelectCommand.Connection = sqlConnection1;
>>>> sda2.SelectCommand.CommandType = CommandType.Text;
>>>> sda2.SelectCommand.CommandText = "select * from Customers";
>>>>
>>>> sda2.Fill(ds1,"Customers");
>>>>
>>>> ddLastName.DataSource = ds1;
>>>> ddLastName.DataMember = "Employees";
>>>> ddLastName.DataTextField = "LastName";
>>>> ddLastName.DataBind();
>>>> ddLastName.Items.Insert(0,"Select:");
>>>>
>>>> // to fill with specific cell data
>>>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>>>> lblSpecific.Text +=
>>>> ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>>>
>>>> // to fill the datagrid
>>>> DataGrid1.DataSource = ds1;
>>>> DataGrid1.DataMember = "Employees";
>>>> DataGrid1.DataBind();
>>>>
>>>> }
>>>> }
>>>>
>>>> #region Web Form Designer generated code
>>>> override protected void OnInit(EventArgs e)
>>>> {
>>>> //
>>>> // CODEGEN: This call is required by the ASP.NET Web Form
>>>> Designer.
>>>> //
>>>> InitializeComponent();
>>>> base.OnInit(e);
>>>> }
>>>>
>>>> /// <summary>
>>>> /// Required method for Designer support - do not modify
>>>> /// the contents of this method with the code editor.
>>>> /// </summary>
>>>> private void InitializeComponent()
>>>> {
>>>> this.ddLastName.SelectedIndexChanged += new
>>>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>>>> this.Load += new System.EventHandler(this.Page_Load);
>>>>
>>>> }
>>>> #endregion
>>>>
>>>> private void ddLastName_SelectedIndexChanged(object sender,
>>>> System.EventArgs e)
>>>> {
>>>> if(ddLastName.SelectedIndex != 0)
>>>> {
>>>> int i = 0;
>>>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>>>> {
>>>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>>>> {
>>>> lblDisplay.Text =
>>>> ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>>>> }
>>>> i++;
>>>> }
>>>> }
>>>> else
>>>> {
>>>> lblDisplay.Text = "";
>>>> }
>>>> }
>>>> }
>>>> }
>>>>
>>>
>>>
>>
>>
>
>



Nov 19 '05 #11
No sweat.

I sent an email to DeveloperFusion, suggesting that they
widen the textbox where the code to be converted is inserted.

That would prevent that problem from occurring
( unless the line is humongously long... ).


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Mr Newbie" <he**@now.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks for the follow up on this Juan. I dont think I would have had the energy

;-)

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uZ**************@TK2MSFTNGP15.phx.gbl...
re:
Many thanks Juan for this utility for conversion.


You're very welcome.

re:
Mr.Newbie:
your code has errors


Actually, it doesn't.

You have to work around the limitations of the conversion tool,
by making sure you insert code-line breaks ( the underscore in VB.NET )
so that the conversion utility doesn't think the code statements are truncated.

So, if you insert underscores at the end of the longest lines,
the conversion tool can understand them.

For example, here's an appropiately marked source segment :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

Me.adaptCalls.Fill(Me.DsCalls1)
Me.adaptContacts.Fill(Me.DsContacts1)
Me.adaptContactTypes.Fill(Me.DsContactTypes1)

AddToCache("adaptCalls", adaptCalls)
AddToCache("adaptContacts", adaptContacts)
AddToCache("adaptContactTypes", adaptContactTypes)

AddToCache("dsCalls", DsCalls1)
AddToCache("dsContacts", DsContacts1)
AddToCache("dsContactTypes", DsContactTypes1)

End If
End Sub

That is correctly converted by the conversion utility to :

private void Page_Load(object sender, System.EventArgs e)
{
if (!(Page.IsPostBack)) {
this.adaptCalls.Fill(this.DsCalls1);
this.adaptContacts.Fill(this.DsContacts1);
this.adaptContactTypes.Fill(this.DsContactTypes1);
AddToCache("adaptCalls", adaptCalls);
AddToCache("adaptContacts", adaptContacts);
AddToCache("adaptContactTypes", adaptContactTypes);
AddToCache("dsCalls", DsCalls1);
AddToCache("dsContacts", DsContacts1);
AddToCache("dsContactTypes", DsContactTypes1);
}
}

btw, the conversion utility may not do a complete conversion,
or even a completely accurate one, but it sure saves a lot of
typing, since -mostly- checking code for errors is much easier
than writing it.

I'm sure some will disagree on this... ;-)


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still, thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
>I am very disturbing, but if you please could you write it again in C# because I
>don't
>understand vb.net.
> Many Thanks.

> "Mr Newbie" <he**@now.com> wrote in message
> news:eH**************@TK2MSFTNGP14.phx.gbl...
>> Here's a little example which uses a helper function. And yes your right about the
>> constructor but that was not meant to be tested code just really to point you in
>> the
>> right direction. In this example, the form is simply a main switchboard which does
>> not use any data, on the other forms in the application they define the dataset,
>> and
>> adapter as class level variables and load them from cache in the Page_Load event as
>> described which can be done with a simple assignment
>> variableName=cache("storedIdentifier").
>>
>> Hope this helps you.
>>
>> Regards - Mr N. . . .
>>
>> ----------------------
>>
>> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
>> System.EventArgs)
>> Handles MyBase.Load
>> 'Put user code to initialize the page here
>>
>> If Not Page.IsPostBack Then
>>
>> Me.adaptCalls.Fill(Me.DsCalls1)
>> Me.adaptContacts.Fill(Me.DsContacts1)
>> Me.adaptContactTypes.Fill(Me.DsContactTypes1)
>>
>> AddToCache("adaptCalls", adaptCalls)
>> AddToCache("adaptContacts", adaptContacts)
>> AddToCache("adaptContactTypes", adaptContactTypes)
>>
>> AddToCache("dsCalls", DsCalls1)
>> AddToCache("dsContacts", DsContacts1)
>> AddToCache("dsContactTypes", DsContactTypes1)
>>
>>
>> End If
>>
>>
>> End Sub
>>
>> Private Sub AddToCache(ByVal name As String, ByVal item As Object)
>> If Not IsNothing(Cache(name)) Then
>> Return
>> Else
>> Cache.Add(name, item, Nothing, DateTime.MaxValue,
>> System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default, Nothing)
>> End If
>> End Sub
>>
>>
>>
>> "Bishoy George" <bi****@bishoy.com> wrote in message
>> news:ul**************@TK2MSFTNGP11.phx.gbl...
>>> You are right. But, please write me more specific coding about caching:
>>> 1- code of caching exactly: as I looked at msdn there is difficult things to set
>>> and
>>> there is no overloading of Cache.Add() that takes 1 parameter as you wrote.
>>> 2- where exactly to put that caching code?
>>> Thanks.
>>>
>>> "Mr Newbie" <he**@now.com> wrote in message
>>> news:uV**************@tk2msftngp13.phx.gbl...
>>>> The problem is that when you click the Dropdown Box, you trigger a postback.
>>>> Unfortunately, your Page_Load event only fills the dataset during the initial
>>>> GET.
>>>> So there is no data to be had when you do your Autopostback.
>>>>
>>>> Once a Webform is constructed and the page rendered, the object is destroyed. You
>>>> need to arrange for the data to be filled on the initial get and saved to the
>>>> cache
>>>> for example.
>>>>
>>>> cache("ds1)= Ds1
>>>>
>>>>
>>>> and as an else condition to your if not IsPostback
>>>>
>>>> ds1 = cache("ds1")
>>>>
>>>> page.DataBind() .. . . . .etc
>>>>
>>>> PS: This is a common mistake to make.
>>>>
>>>> HTH
>>>>
>>>>
>>>>
>>>> "Bishoy George" <bi****@bishoy.com> wrote in message
>>>> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>>>>I have a dataset called ds1 filled with 2 tables Employees and Customers from
>>>>>Northwind database.
>>>>> I have dropdownList called ddLastName with the following properties:
>>>>>
>>>>> ddLastName.DataSource = ds1;
>>>>> ddLastName.DataMember = "Employees";
>>>>> ddLastName.DataTextField = "LastName";
>>>>> ddLastName.DataBind();
>>>>> ddLastName.Items.Insert(0,"Select:");
>>>>>
>>>>> I have a label called lblDisplay
>>>>>
>>>>> I want the label to display the Notes column data of the selected item in the
>>>>> dropdownList.
>>>>>
>>>>> My code is causing a NullReferenceException.
>>>>> Can anyone find out what is the problem please?
>>>>>
>>>>> ------------------------------------
>>>>> Code: ----------------------------------------------------
>>>>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false"
>>>>> Inherits="WebApplication2.WebForm9" %>
>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>>>>> <HTML>
>>>>> <HEAD>
>>>>> <title>WebForm9</title>
>>>>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>>>>> <meta content="C#" name="CODE_LANGUAGE">
>>>>> <meta content="JavaScript" name="vs_defaultClientScript">
>>>>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>>>>> name="vs_targetSchema">
>>>>> </HEAD>
>>>>> <body MS_POSITIONING="GridLayout">
>>>>> <form id="Form1" method="post" runat="server">
>>>>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px; POSITION:
>>>>> absolute;
>>>>> TOP: 19px"
>>>>> runat="server" Width="536px" Height="8px"></asp:Label>
>>>>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px; POSITION:
>>>>> absolute; TOP: 152px"
>>>>> runat="server" Width="672px" Height="120px" BorderColor="#3366CC"
>>>>> BorderStyle="None" BorderWidth="1px"
>>>>> BackColor="White" CellPadding="4">
>>>>> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
>>>>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>>>>> BackColor="#009999"></SelectedItemStyle>
>>>>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>>>>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>>>>> BackColor="#003399"></HeaderStyle>
>>>>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC"
>>>>> Mode="NumericPages"></PagerStyle>
>>>>> </asp:DataGrid>
>>>>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px; POSITION:
>>>>> absolute; TOP: 16px"
>>>>> runat="server" Width="112px" Height="24px"
>>>>> AutoPostBack="True"></asp:DropDownList>
>>>>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute;
>>>>> TOP: 48px" runat="server"
>>>>> Width="272px" Height="16px" ForeColor="Red">I want to display notes of
>>>>> selected
>>>>> employee</asp:Label>
>>>>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px; POSITION:
>>>>> absolute;
>>>>> TOP: 72px"
>>>>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>>>>> </body>
>>>>> </HTML>
>>>>>
>>>>> --------------------------------------------------code
>>>>> behind--------------------------------------------------------------------------
>>>>> using System;
>>>>> using System.Collections;
>>>>> using System.ComponentModel;
>>>>> using System.Data;
>>>>> using System.Data.SqlClient;
>>>>> using System.Drawing;
>>>>> using System.Web;
>>>>> using System.Web.SessionState;
>>>>> using System.Web.UI;
>>>>> using System.Web.UI.WebControls;
>>>>> using System.Web.UI.HtmlControls;
>>>>>
>>>>> namespace WebApplication2
>>>>> {
>>>>> /// <summary>
>>>>> /// Summary description for WebForm9.
>>>>> /// </summary>
>>>>> public class WebForm9 : System.Web.UI.Page
>>>>> {
>>>>> protected DataSet ds1 = new DataSet();
>>>>> protected System.Web.UI.WebControls.Label lblDisplay;
>>>>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>>>>> protected System.Web.UI.WebControls.Label lblText;
>>>>> protected System.Web.UI.WebControls.Label lblSpecific;
>>>>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>>>>
>>>>> private void Page_Load(object sender, System.EventArgs e)
>>>>> {
>>>>> if(!Page.IsPostBack)
>>>>> {
>>>>> string connectionString = "data source =localhost; initial catalog
>>>>> =Northwind;
>>>>> integrated security=true;";
>>>>> SqlConnection sqlConnection1 = new SqlConnection(connectionString);
>>>>>
>>>>> SqlCommand sqlCommand1 = new SqlCommand();
>>>>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>>>>
>>>>> // "dataset defined at class level"
>>>>> // DataSet ds1 = new DataSet();
>>>>>
>>>>> sda1.SelectCommand = sqlCommand1;
>>>>> sda1.SelectCommand.Connection = sqlConnection1;
>>>>> sda1.SelectCommand.CommandType = CommandType.Text;
>>>>> sda1.SelectCommand.CommandText = "select * from Employees";
>>>>>
>>>>> sda1.Fill(ds1,"Employees");
>>>>>
>>>>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>>>>
>>>>> sda2.SelectCommand = sqlCommand1;
>>>>> sda2.SelectCommand.Connection = sqlConnection1;
>>>>> sda2.SelectCommand.CommandType = CommandType.Text;
>>>>> sda2.SelectCommand.CommandText = "select * from Customers";
>>>>>
>>>>> sda2.Fill(ds1,"Customers");
>>>>>
>>>>> ddLastName.DataSource = ds1;
>>>>> ddLastName.DataMember = "Employees";
>>>>> ddLastName.DataTextField = "LastName";
>>>>> ddLastName.DataBind();
>>>>> ddLastName.Items.Insert(0,"Select:");
>>>>>
>>>>> // to fill with specific cell data
>>>>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>>>>> lblSpecific.Text += ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>>>>
>>>>> // to fill the datagrid
>>>>> DataGrid1.DataSource = ds1;
>>>>> DataGrid1.DataMember = "Employees";
>>>>> DataGrid1.DataBind();
>>>>>
>>>>> }
>>>>> }
>>>>>
>>>>> #region Web Form Designer generated code
>>>>> override protected void OnInit(EventArgs e)
>>>>> {
>>>>> //
>>>>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
>>>>> //
>>>>> InitializeComponent();
>>>>> base.OnInit(e);
>>>>> }
>>>>>
>>>>> /// <summary>
>>>>> /// Required method for Designer support - do not modify
>>>>> /// the contents of this method with the code editor.
>>>>> /// </summary>
>>>>> private void InitializeComponent()
>>>>> {
>>>>> this.ddLastName.SelectedIndexChanged += new
>>>>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>>>>> this.Load += new System.EventHandler(this.Page_Load);
>>>>>
>>>>> }
>>>>> #endregion
>>>>>
>>>>> private void ddLastName_SelectedIndexChanged(object sender, System.EventArgs e)
>>>>> {
>>>>> if(ddLastName.SelectedIndex != 0)
>>>>> {
>>>>> int i = 0;
>>>>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>>>>> {
>>>>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>>>>> {
>>>>> lblDisplay.Text = ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>>>>> }
>>>>> i++;
>>>>> }
>>>>> }
>>>>> else
>>>>> {
>>>>> lblDisplay.Text = "";
>>>>> }
>>>>> }
>>>>> }
>>>>> }
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Nov 19 '05 #12
Not any code that works somewhere will work everywhere:
The caching idea is true, but the needed code is just:
Cache["dataset1"] = ds1; // in the block of assigning the dataset object

and

ds1 = Cache["dataset1"] as DataSet; // in the block where the dataset will
be used

Thanks

---------------------------------------------------------------------------------------
"Mr Newbie" <he**@now.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
My code does NOT have errors. This is from a working project.

"Bishoy George" <bi****@bishoy.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
Many thanks Juan for this utility for conversion.

Mr.Newbie:
your code has errors so I can't understand it or even convert it. Still,
thank you.

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/uti...btocsharp.aspx

will convert VB.NET to C# ( and C# to VB.NET ) for you.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Bishoy George" <bi****@bishoy.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
> Here's a little example which uses a helper function. And yes your
> right about the constructor but that was not meant to be tested code
> just really to point you in the right direction. In this example, the
> form is simply a main switchboard which does not use any data, on the
> other forms in the application they define the dataset, and adapter as
> class level variables and load them from cache in the Page_Load event
> as described which can be done with a simple assignment
> variableName=cache("storedIdentifier").
>
> Hope this helps you.
>
> Regards - Mr N. . . .
>
> ----------------------
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> 'Put user code to initialize the page here
>
> If Not Page.IsPostBack Then
>
> Me.adaptCalls.Fill(Me.DsCalls1)
> Me.adaptContacts.Fill(Me.DsContacts1)
> Me.adaptContactTypes.Fill(Me.DsContactTypes1)
>
> AddToCache("adaptCalls", adaptCalls)
> AddToCache("adaptContacts", adaptContacts)
> AddToCache("adaptContactTypes", adaptContactTypes)
>
> AddToCache("dsCalls", DsCalls1)
> AddToCache("dsContacts", DsContacts1)
> AddToCache("dsContactTypes", DsContactTypes1)
>
>
> End If
>
>
> End Sub
>
> Private Sub AddToCache(ByVal name As String, ByVal item As Object)
> If Not IsNothing(Cache(name)) Then
> Return
> Else
> Cache.Add(name, item, Nothing, DateTime.MaxValue,
> System.TimeSpan.FromDays(20), Caching.CacheItemPriority.Default,
> Nothing)
> End If
> End Sub
>
>
>
> "Bishoy George" <bi****@bishoy.com> wrote in message
> news:ul**************@TK2MSFTNGP11.phx.gbl...
>> You are right. But, please write me more specific coding about
>> caching:
>> 1- code of caching exactly: as I looked at msdn there is difficult
>> things to set and there is no overloading of Cache.Add() that takes 1
>> parameter as you wrote.
>> 2- where exactly to put that caching code?
>> Thanks.
>>
>> "Mr Newbie" <he**@now.com> wrote in message
>> news:uV**************@tk2msftngp13.phx.gbl...
>>> The problem is that when you click the Dropdown Box, you trigger a
>>> postback. Unfortunately, your Page_Load event only fills the dataset
>>> during the initial GET. So there is no data to be had when you do
>>> your Autopostback.
>>>
>>> Once a Webform is constructed and the page rendered, the object is
>>> destroyed. You need to arrange for the data to be filled on the
>>> initial get and saved to the cache for example.
>>>
>>> cache("ds1)= Ds1
>>>
>>>
>>> and as an else condition to your if not IsPostback
>>>
>>> ds1 = cache("ds1")
>>>
>>> page.DataBind() .. . . . .etc
>>>
>>> PS: This is a common mistake to make.
>>>
>>> HTH
>>>
>>>
>>>
>>> "Bishoy George" <bi****@bishoy.com> wrote in message
>>> news:uN****************@TK2MSFTNGP14.phx.gbl...
>>>>I have a dataset called ds1 filled with 2 tables Employees and
>>>>Customers from Northwind database.
>>>> I have dropdownList called ddLastName with the following
>>>> properties:
>>>>
>>>> ddLastName.DataSource = ds1;
>>>> ddLastName.DataMember = "Employees";
>>>> ddLastName.DataTextField = "LastName";
>>>> ddLastName.DataBind();
>>>> ddLastName.Items.Insert(0,"Select:");
>>>>
>>>> I have a label called lblDisplay
>>>>
>>>> I want the label to display the Notes column data of the selected
>>>> item in the dropdownList.
>>>>
>>>> My code is causing a NullReferenceException.
>>>> Can anyone find out what is the problem please?
>>>>
>>>> ------------------------------------
>>>> Code: ----------------------------------------------------
>>>> <%@ Page language="c#" Codebehind="WebForm9.aspx.cs"
>>>> AutoEventWireup="false" Inherits="WebApplication2.WebForm9" %>
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
>>>> <HTML>
>>>> <HEAD>
>>>> <title>WebForm9</title>
>>>> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
>>>> <meta content="C#" name="CODE_LANGUAGE">
>>>> <meta content="JavaScript" name="vs_defaultClientScript">
>>>> <meta content="http://schemas.microsoft.com/intellisense/ie5"
>>>> name="vs_targetSchema">
>>>> </HEAD>
>>>> <body MS_POSITIONING="GridLayout">
>>>> <form id="Form1" method="post" runat="server">
>>>> <asp:Label id="lblDisplay" style="Z-INDEX: 101; LEFT: 144px;
>>>> POSITION: absolute; TOP: 19px"
>>>> runat="server" Width="536px" Height="8px"></asp:Label>
>>>> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 24px;
>>>> POSITION: absolute; TOP: 152px"
>>>> runat="server" Width="672px" Height="120px"
>>>> BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
>>>> BackColor="White" CellPadding="4">
>>>> <FooterStyle ForeColor="#003399"
>>>> BackColor="#99CCCC"></FooterStyle>
>>>> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99"
>>>> BackColor="#009999"></SelectedItemStyle>
>>>> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
>>>> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF"
>>>> BackColor="#003399"></HeaderStyle>
>>>> <PagerStyle HorizontalAlign="Left" ForeColor="#003399"
>>>> BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
>>>> </asp:DataGrid>
>>>> <asp:DropDownList id="ddLastName" style="Z-INDEX: 103; LEFT:
>>>> 16px; POSITION: absolute; TOP: 16px"
>>>> runat="server" Width="112px" Height="24px"
>>>> AutoPostBack="True"></asp:DropDownList>
>>>> <asp:Label id="lblText" style="Z-INDEX: 104; LEFT: 144px;
>>>> POSITION: absolute; TOP: 48px" runat="server"
>>>> Width="272px" Height="16px" ForeColor="Red">I want to display
>>>> notes of selected employee</asp:Label>
>>>> <asp:Label id="lblSpecific" style="Z-INDEX: 105; LEFT: 16px;
>>>> POSITION: absolute; TOP: 72px"
>>>> runat="server" Width="704px" Height="56px"></asp:Label></form>
>>>> </body>
>>>> </HTML>
>>>>
>>>> --------------------------------------------------code
>>>> behind--------------------------------------------------------------------------
>>>> using System;
>>>> using System.Collections;
>>>> using System.ComponentModel;
>>>> using System.Data;
>>>> using System.Data.SqlClient;
>>>> using System.Drawing;
>>>> using System.Web;
>>>> using System.Web.SessionState;
>>>> using System.Web.UI;
>>>> using System.Web.UI.WebControls;
>>>> using System.Web.UI.HtmlControls;
>>>>
>>>> namespace WebApplication2
>>>> {
>>>> /// <summary>
>>>> /// Summary description for WebForm9.
>>>> /// </summary>
>>>> public class WebForm9 : System.Web.UI.Page
>>>> {
>>>> protected DataSet ds1 = new DataSet();
>>>> protected System.Web.UI.WebControls.Label lblDisplay;
>>>> protected System.Web.UI.WebControls.DataGrid DataGrid1;
>>>> protected System.Web.UI.WebControls.Label lblText;
>>>> protected System.Web.UI.WebControls.Label lblSpecific;
>>>> protected System.Web.UI.WebControls.DropDownList ddLastName;
>>>>
>>>> private void Page_Load(object sender, System.EventArgs e)
>>>> {
>>>> if(!Page.IsPostBack)
>>>> {
>>>> string connectionString = "data source =localhost; initial
>>>> catalog =Northwind; integrated security=true;";
>>>> SqlConnection sqlConnection1 = new
>>>> SqlConnection(connectionString);
>>>>
>>>> SqlCommand sqlCommand1 = new SqlCommand();
>>>> SqlDataAdapter sda1 = new SqlDataAdapter();
>>>>
>>>> // "dataset defined at class level"
>>>> // DataSet ds1 = new DataSet();
>>>>
>>>> sda1.SelectCommand = sqlCommand1;
>>>> sda1.SelectCommand.Connection = sqlConnection1;
>>>> sda1.SelectCommand.CommandType = CommandType.Text;
>>>> sda1.SelectCommand.CommandText = "select * from Employees";
>>>>
>>>> sda1.Fill(ds1,"Employees");
>>>>
>>>> SqlDataAdapter sda2 = new SqlDataAdapter();
>>>>
>>>> sda2.SelectCommand = sqlCommand1;
>>>> sda2.SelectCommand.Connection = sqlConnection1;
>>>> sda2.SelectCommand.CommandType = CommandType.Text;
>>>> sda2.SelectCommand.CommandText = "select * from Customers";
>>>>
>>>> sda2.Fill(ds1,"Customers");
>>>>
>>>> ddLastName.DataSource = ds1;
>>>> ddLastName.DataMember = "Employees";
>>>> ddLastName.DataTextField = "LastName";
>>>> ddLastName.DataBind();
>>>> ddLastName.Items.Insert(0,"Select:");
>>>>
>>>> // to fill with specific cell data
>>>> lblSpecific.Text = "Displaying specific Data:<br><br>";
>>>> lblSpecific.Text +=
>>>> ds1.Tables["Employees"].Rows[0]["Notes"].ToString();
>>>>
>>>> // to fill the datagrid
>>>> DataGrid1.DataSource = ds1;
>>>> DataGrid1.DataMember = "Employees";
>>>> DataGrid1.DataBind();
>>>>
>>>> }
>>>> }
>>>>
>>>> #region Web Form Designer generated code
>>>> override protected void OnInit(EventArgs e)
>>>> {
>>>> //
>>>> // CODEGEN: This call is required by the ASP.NET Web Form
>>>> Designer.
>>>> //
>>>> InitializeComponent();
>>>> base.OnInit(e);
>>>> }
>>>>
>>>> /// <summary>
>>>> /// Required method for Designer support - do not modify
>>>> /// the contents of this method with the code editor.
>>>> /// </summary>
>>>> private void InitializeComponent()
>>>> {
>>>> this.ddLastName.SelectedIndexChanged += new
>>>> System.EventHandler(this.ddLastName_SelectedIndexC hanged);
>>>> this.Load += new System.EventHandler(this.Page_Load);
>>>>
>>>> }
>>>> #endregion
>>>>
>>>> private void ddLastName_SelectedIndexChanged(object sender,
>>>> System.EventArgs e)
>>>> {
>>>> if(ddLastName.SelectedIndex != 0)
>>>> {
>>>> int i = 0;
>>>> foreach(DataRow r in ds1.Tables["Employees"].Rows)
>>>> {
>>>> if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
>>>> {
>>>> lblDisplay.Text =
>>>> ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
>>>> }
>>>> i++;
>>>> }
>>>> }
>>>> else
>>>> {
>>>> lblDisplay.Text = "";
>>>> }
>>>> }
>>>> }
>>>> }
>>>>
>>>
>>>
>>
>>
>
>



Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Harvey | last post by:
Hi everyone, I was wondering if it is possible, to use SQL to return more than one table at a time into a dataset. I only know the basics of SQL and so I'm not sure if I'm just asking a stupid...
2
by: JS | last post by:
I'm trying to create a data layer and having problems returning a DataSet from my code that's in a class module. Please forgive me. I'm new to C# (VB'er). I decided to create my data layer in...
2
by: Simon Harvey | last post by:
Hi everyone, I was wondering if it is possible, to use SQL to return more than one table at a time into a dataset. I only know the basics of SQL and so I'm not sure if I'm just asking a stupid...
0
by: Mike | last post by:
Hi, I am trying to solve a problem with a DataSet issue I am having. In my app the user selects certain options and based on those options it goes out to the db to get data based on those...
15
by: JIM.H. | last post by:
Hello, Can I send a dataset as a parameter into stored procedure and import data to a table in the stored procedure? Thanks, Jim.
2
by: Jeff Brown | last post by:
OK i have came to the conclusion that since this app will be run on multiple computers that loading a complete database and then updating on exit is a moot point. I have tried several ideas...
4
by: markerussell | last post by:
I have a question, in that if i have an application is it better to have one dataset per data adapter, ie daCustomerTable -> dsCustomer, daOrders -> dsOrders etc. or to say have a couple of dataset...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
2
by: JT | last post by:
I have an SQL database with one table in it. I want to load the table into a DataSet, add new records to the DataSet, then commit the changes back to the database. Using C#.NET, the following...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.