dataset question

B

Bishoy George

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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}
 
B

Bishoy George

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 said:
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 said:
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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}

 
B

Bishoy George

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 said:
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 said:
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 said:
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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
M

Mr Newbie

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 said:
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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}
 
M

Mr Newbie

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 said:
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 said:
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 said:
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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
M

Mr Newbie

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 said:
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 said:
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 said:
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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
J

Juan T. Llibre

Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




Bishoy George said:
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 said:
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 said:
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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
B

Bishoy George

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 said:
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




Bishoy George said:
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 said:
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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
J

Juan T. Llibre

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





Bishoy George said:
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 said:
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




Bishoy George said:
I am very disturbing, but if you please could you write it again in C# because I don't
understand vb.net.
Many Thanks.
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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
M

Mr Newbie

My code does NOT have errors. This is from a working project.



Bishoy George said:
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 said:
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




Bishoy George said:
I am very disturbing, but if you please could you write it again in C#
because I don't understand vb.net.
Many Thanks.
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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
M

Mr Newbie

Thanks for the follow up on this Juan. I dont think I would have had the
energy

;-)



Juan T. Llibre said:
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... ;-)





Bishoy George said:
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 said:
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




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

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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
J

Juan T. Llibre

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





Mr Newbie said:
Thanks for the follow up on this Juan. I dont think I would have had the energy

;-)



Juan T. Llibre said:
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... ;-)





Bishoy George said:
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.



Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




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

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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 
B

Bishoy George

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 said:
My code does NOT have errors. This is from a working project.



Bishoy George said:
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 said:
Why don't you use an online conversion utility ?

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

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




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

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



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.

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



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_SelectedIndexChanged);
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["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top