By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

dataset question

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.