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
ataGrid 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
ataGrid>
<asp
ropDownList id="ddLastName" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp
ropDownList>
<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 = "";
}
}
}
}
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
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
<asp
POSITION: absolute; TOP: 16px"
runat="server" Width="112px" Height="24px"
AutoPostBack="True"></asp
<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 = "";
}
}
}
}