数据集问题 [英] dataset question

查看:78
本文介绍了数据集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为ds1的数据集,其中包含来自Northwind数据库的2个表Employees和Customers



我有dropdownList,名为ddLastName,具有以下属性:


ddLastName.DataSource = ds1;

ddLastName.DataMember =" Employees";

ddLastName.DataTextField =" LastName";

ddLastName.DataBind();

ddLastName.Items.Insert(0," Select:");


我有一个名为lblDisplay的标签


我希望标签显示所选项目的Notes列数据在

下拉列表中。


我的代码导致NullReferenceException。

有谁可以找出问题是什么?


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

代码:------------- ---------------------------------------

<% @ Page language =" c#"代码隐藏= QUOT; 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中"方法= QUOT;交" runat =" server">

< asp:Label id =" lblDisplay" style =" Z-INDEX:101;左:144px;位置:

绝对值; TOP:19px"

runat =" server"宽度= QUOT; 536px"高度=8px>< / asp:标签>

< asp:DataGrid id =" DataGrid1" style =" Z-INDEX:102;左:24px;位置:

绝对值; TOP:152px"

runat =" server"宽度= QUOT; 672px"高度= QUOT; 120像素" 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 Horizo​​ntalAlign =" Left" ForeColor ="#003399"

BackColor ="#99CCCC" Mode =" NumericPages">< / PagerStyle>

< / asp:DataGrid>

< asp:DropDownList id =" ddLastName" style =" Z-INDEX:103;左:16px;

位置:绝对; TOP:16px"

runat =" server"宽度= QUOT; 112px"高度=24px

AutoPostBack =" True">< / asp:DropDownList>

< asp:Label id =" lblText" style =" Z-INDEX:104;左:144px;位置:

绝对值; TOP:48px runat =" server"

Width =" 272px"高度= QUOT; 16px的" ForeColor =" Red">我想显示

所选员工的备注< / asp:Label>

< asp:Label id =" lblSpecific" style =" Z-INDEX:105;左:16px;位置:

绝对值; TOP:72px"

runat =" server"宽度= QUOT; 704px"高度=56px>< / asp:标签>< /形式>

< / body>

< / HTML>


------------------------------------------ --------代码

落后-------------------------------- ------------------------------------------

使用System;

使用System.Collections;

使用System.ComponentModel;

使用System.Data;

使用System.Data.SqlClient;

使用System.Drawing;

使用System.Web;

使用System.Web.SessionState;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Web.UI.HtmlControls;


名称空间WebApplication2

{

///< summary>

/// WebForm9的摘要说明。

///< / summary>

公共类WebForm9:System.Web.UI.Page

{

protected DataSet ds1 = new DataSet();

protected System.Web.UI.WebContr ols.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 (对象发送者,System.EventArgs e)

{

if(!Page.IsPostBack)

{

string connectionString =" data source = localhost;初始目录

= Northwind; integrated security = true;" ;;

SqlConnection sqlConnection1 = new SqlConnection(connectionString);


SqlCommand sqlCommand1 = new SqlCommand();

SqlDataAdapter sda1 = new SqlDataAdapter();


//在类级别定义的数据集

// 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,& ;客户);


ddLastName.DataSource = ds1;

ddLastName.DataMember =" Employees";

ddLastName .DataTextField =" LastName";

ddLastName.DataBind();

ddLastName.Items.Insert(0," Select:");


//填写特定的单元数据

lblSpecific.Text ="显示特定数据:< br>< br>";

lblSpecific.Text + = ds1.Tables [" Employees"]。行[0] [" Notes"]。ToString();


//来填充datagrid

DataGrid1.DataSource = ds1;

DataGrid1.DataMember =" Employees";

DataGrid1.DataBind();


}

}


#region Web表单设计器生成的代码

覆盖protected void OnInit( EventArgs e)

{

//

// CODEGEN:ASP.NET Web窗体设计器需要此调用。

//

InitializeComponen t();

base.OnInit(e);

}


///< summary>

/// Designer支持所需的方法 - 不要使用代码编辑器修改

///此方法的内容。

///< ; / summary>

private void InitializeComponent()

{

this.ddLastName.SelectedIndexChanged + = new

System.EventHandler(this.ddLastName_SelectedIndexC hanged);

this.Load + = new System.EventHandler(this.Page_Load);


}

#endregion


private void ddLastName_SelectedIndexChanged(对象发件人,

System.EventArgs e)

{

if(ddLastName.SelectedIndex!= 0)

{

int i = 0;

foreach(dsR中的DataRow r) .Tables [" Employees"]。行)

{

if(r [" LastName"]。ToString()== ddLastName.SelectedItem.Text)

{

lblDisplay.Text = ds1.Tables [" Employees"]。行[i] ["注释"]。ToString();

}

i ++;

}

}

其他

{

lblDisplay.Text ="" ;;

}

}

}

}

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

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

I have a label called lblDisplay

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

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

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

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

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

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

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

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

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

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

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

sda2.Fill(ds1,"Customers");

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

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

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

}
}

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

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

}
#endregion

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

推荐答案

问题是当你点击下拉框时,你会触发回发。

不幸的是,您的Page_Load事件仅在

初始GET期间填充数据集。因此,当您进行Autopostback时,没有数据。


构建Webform并呈现页面后,对象将被销毁。你需要安排在初始get

上填写数据并保存到缓存中。


cache(" ds1)= Ds1

以及作为其他条件,如果不是IsPostback


ds1 = cache(" ds1")


page.DataBind().. 。 。 .etc


PS:这是一个常见的错误。


HTH


" ; Bishoy George" <双**** @ bishoy.com>在留言中写道

news:uN **************** @ TK2MSFTNGP14.phx.gbl ...
The problem is that when you click the Dropdown Box, you trigger a postback.
Unfortunately, your Page_Load event only fills the dataset during the
initial GET. So there is no data to be had when you do your Autopostback.

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

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

ds1 = cache("ds1")

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

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
我有一个名为ds1的数据集填充了来自Northwind数据库的2个表Employees和Customers
我有一个名为ddLastName的dropdownList,具有以下属性:

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

我有一个名为lblDisplay的标签

我希望标签在下拉列表中显示所选项目的Notes列数据。

我的代码导致NullReferenceException。
任何人都可以找出问题是什么吗?

----------------- -------------------
代码:------------------------- ---------------------------
<%@ Page language =" c#" Codebehind =" WebForm9.aspx.cs"
AutoEventWireup =" false"继承= QUOT; 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"方法= QUOT;交" runat =" server">
< asp:Label id =" lblDisplay" style =" Z-INDEX:101;左:144px;位置:
绝对; TOP:19px"
runat =" server"宽度= QUOT; 536px"高度=8px>< / asp:标签>
< asp:DataGrid id =" DataGrid1" style =" Z-INDEX:102;左:24px;位置:
绝对; TOP:152px"
runat =" server"宽度= QUOT; 672px"高度= QUOT; 120像素" 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 Horizo​​ntalAlign =" Left" ForeColor ="#003399"
BackColor ="#99CCCC" Mode =" NumericPages">< / PagerStyle>
< / asp:DataGrid>
< asp:DropDownList id =" ddLastName" style =" Z-INDEX:103;左:16px;
位置:绝对; TOP:16px"
runat =" server"宽度= QUOT; 112px"高度=24px
AutoPostBack =" True">< / asp:DropDownList>
< asp:Label id =" lblText" style =" Z-INDEX:104;左:144px;位置:
绝对; TOP:48px runat =" server"
Width =" 272px"高度= QUOT; 16px的" ForeColor =" Red">我想显示所选员工的备注< / asp:Label>
< asp:Label id =" lblSpecific" style =" Z-INDEX:105;左:16px;位置:
绝对; TOP:72px"
runat =" server"宽度= QUOT; 704px"高度=56px>< / asp:标签>< / form>
< / body>
< / HTML>

---- ---------------------------------------------- code
背后------------------------------------------------ --------------------------
使用System;
使用System.Collections;
使用System.ComponentModel ;
使用System.Data;
使用System.Data.SqlClient;
使用System.Drawing;
使用System.Web;
使用System.Web.SessionState;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.HtmlControls;

命名空间WebApplication2
{
///< summary>
/// WebForm9的摘要说明。
///< / summary>
公共类WebForm9:System.Web.UI .Page
受保护的DataSet ds1 = new DataSet();
受保护的System.Web.UI.WebControls.Label lblDisplay;
受保护的System.Web.UI.WebControls.DataGrid DataGrid1;
受保护的System.Web.UI.WebC ontrols.Label lblText;
受保护的System.Web.UI.WebControls.Label lblSpecific;
受保护的System.Web.UI.WebControls.DropDownList ddLastName;

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

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

//在类级别定义的数据集
// 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" ;);

d dLastName.DataSource = ds1;
ddLastName.DataMember =" Employees";
ddLastName.DataTextField =" LastName";
ddLastName.DataBind();
ddLastName.Items。插入(0,选择:);

//以填充特定的单元格数据
lblSpecific.Text ="显示特定数据:< br>< br> " ;;
lblSpecific.Text + =
ds1.Tables [" Employees"]。行[0] [" Notes"]。ToString();

/ /来填充数据网格
DataGrid1.DataSource = ds1;
DataGrid1.DataMember =" Employees";
DataGrid1.DataBind();

}
}

#region Web窗体设计器生成的代码
覆盖保护的void OnInit(EventArgs e)
{
//
// CODEGEN:此调用ASP.NET Web窗体设计器需要它。
//
InitializeComponent();
base.OnInit(e);
}

/ //< summary>
///必需的方法f或Designer支持 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged + = new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load + = new System.EventHandler(this.Page_Load);
<私有空ddLastName_SelectedIndexChanged(对象发送者,
System.EventArgs e)
如果(ddLastName.SelectedIndex!=) 0)
{i / 0;
foreach(在ds1.Tables [" Employees"]中的DataRow r。行。)
{
if(r [ " LastName"]。ToString()== ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables [" Employees"]。行[i] ["。 ; Notes"]。ToString();
}
i ++;
}
}

{
lblDisplay.Text =" ;
}
}
}
}
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

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

I have a label called lblDisplay

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

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

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

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

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

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

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

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

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

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

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

sda2.Fill(ds1,"Customers");

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

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

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

}
}

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

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

}
#endregion

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



你是对的。但是,请给我写一些关于缓存的更具体的编码:

1-完全缓存的代码:当我看到msdn时,有很难设置

设置并且没有超载当你需要1个参数的Cache.Add()时,

写道。

2-究竟要放哪个缓存代码?

谢谢。


Mr Newbie <他** @ now.com>在消息中写道

新闻:uV ************** @ tk2msftngp13.phx.gbl ...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things to
set and there is no overloading of Cache.Add() that takes 1 parameter as you
wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
问题在于你单击下拉框,你会触发一个
回发。不幸的是,您的Page_Load事件仅在初始GET期间填充数据集。因此,当您进行自动回溯时,没有数据可用。

一旦构建了Webform并呈现了页面,该对象就会被破坏。例如,您需要安排在初始
获取时填写数据并保存到缓存中。

缓存(" ds1)= Ds1

如果不是IsPostback则作为其他条件

ds1 = cache(" ds1")

page.DataBind().. 。 。 。PS:这是一个常见的错误。

HTH

Bishoy George <双**** @ bishoy.com>在消息中写道
新闻:uN **************** @ TK2MSFTNGP14.phx.gbl ...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

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

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

ds1 = cache("ds1")

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

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
我有一个名为ds1的数据集填充来自Northwind数据库的2个表员工和客户
我有一个名为ddLastName的dropdownList,具有以下属性:

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

我有一个名为lblDisplay的标签

我希望标签在下拉列表中显示所选项目的Notes列数据。

我的代码导致NullReferenceException。
任何人都可以找出问题是什么吗?

--------------------- ---------------
代码:----------------------------- -----------------------
<%@ Page language =" c#" Codebehind =" WebForm9.aspx.cs"
AutoEventWireup =" false"继承= QUOT; 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"方法= QUOT;交" runat =" server">
< asp:Label id =" lblDisplay" style =" Z-INDEX:101;左:144px;位置:
绝对; TOP:19px"
runat =" server"宽度= QUOT; 536px"高度=8px>< / asp:标签>
< asp:DataGrid id =" DataGrid1" style =" Z-INDEX:102;左:24px;位置:
绝对; TOP:152px"
runat =" server"宽度= QUOT; 672px"高度= QUOT; 120像素" 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 Horizo​​ntalAlign =" Left" ForeColor ="#003399"
BackColor ="#99CCCC" Mode =" NumericPages">< / PagerStyle>
< / asp:DataGrid>
< asp:DropDownList id =" ddLastName" style =" Z-INDEX:103;左:16px;
位置:绝对; TOP:16px"
runat =" server"宽度= QUOT; 112px"高度=24px
AutoPostBack =" True">< / asp:DropDownList>
< asp:Label id =" lblText" style =" Z-INDEX:104;左:144px;位置:
绝对; TOP:48px runat =" server"
Width =" 272px"高度= QUOT; 16px的" ForeColor =" Red">我想显示所选员工的备注< / asp:Label>
< asp:Label id =" lblSpecific" style =" Z-INDEX:105;左:16px;位置:
绝对; TOP:72px"
runat =" server"宽度= QUOT; 704px"高度=56px>< / asp:标签>< / form>
< / body>
< / HTML>

---- ---------------------------------------------- code
背后------------------------------------------------ --------------------------
使用System;
使用System.Collections;
使用System.ComponentModel ;
使用System.Data;
使用System.Data.SqlClient;
使用System.Drawing;
使用System.Web;
使用System.Web.SessionState;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.HtmlControls;

命名空间WebApplication2
{
///< summary>
/// WebForm9的摘要说明。
///< / summary>
公共类WebForm9:System.Web.UI .Page
受保护的DataSet ds1 = new DataSet();
受保护的System.Web.UI.WebControls.Label lblDisplay;
受保护的System.Web.UI.WebControls.DataGrid DataGrid1;
受保护的System.Web.UI.WebC ontrols.Label lblText;
受保护的System.Web.UI.WebControls.Label lblSpecific;
受保护的System.Web.UI.WebControls.DropDownList ddLastName;

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

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

//在类级别定义的数据集
// 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" ;);

d dLastName.DataSource = ds1;
ddLastName.DataMember =" Employees";
ddLastName.DataTextField =" LastName";
ddLastName.DataBind();
ddLastName.Items。插入(0,选择:);

//以填充特定的单元格数据
lblSpecific.Text ="显示特定数据:< br>< br> " ;;
lblSpecific.Text + =
ds1.Tables [" Employees"]。行[0] [" Notes"]。ToString();

/ /来填充数据网格
DataGrid1.DataSource = ds1;
DataGrid1.DataMember =" Employees";
DataGrid1.DataBind();

}
}

#region Web窗体设计器生成的代码
覆盖保护的void OnInit(EventArgs e)
{
//
// CODEGEN:此调用ASP.NET Web窗体设计器需要它。
//
InitializeComponent();
base.OnInit(e);
}

/ //< summary>
///必需的方法f或Designer支持 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged + = new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load + = new System.EventHandler(this.Page_Load);
<私有空ddLastName_SelectedIndexChanged(对象发送者,
System.EventArgs e)
如果(ddLastName.SelectedIndex!=) 0)
{i / 0;
foreach(在ds1.Tables [" Employees"]中的DataRow r。行。)
{
if(r [ " LastName"]。ToString()== ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables [" Employees"]。行[i] ["。 ; Notes"]。ToString();
}
i ++;
}
}

{
lblDisplay.Text =" ;
}
}
}
}
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

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

I have a label called lblDisplay

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

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

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

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

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

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

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

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

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

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

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

sda2.Fill(ds1,"Customers");

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

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

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

}
}

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

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

}
#endregion

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




这是一个使用辅助函数的小例子。是的,你的权利

关于构造函数,但这并不意味着测试代码只是真的

指向正确的方向。在这个例子中,表单只是一个不使用任何数据的

主交换机,在他们定义数据集的

应用程序中的其他表单上,以及作为类的适配器级别变量

并在Page_Load事件中从缓存中加载它们,如上所述,可以使用简单的赋值variableName = cache(storedIdentifier)来完成。


希望这会对你有所帮助。


问候 - N先生。 。 。


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


Private Sub Page_Load (ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

''将用户代码置于此处初始化页面


如果不是Page.IsPostBack那么


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)

结束如果

结束Sub $ />

Private Sub AddToCache(ByVal name As String,ByVal item As Object)

If Not IsNothing(Cache(name))然后

返回

Else

Cache.Add(name,item,Nothing,DateTime.MaxValue,

System.TimeSpan.FromDays(20),缓存.CacheItemPriority.Default,Nothing)

结束如果

结束子


" Bishoy George" <双**** @ bishoy.com>在消息中写道

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

Hope this helps you.

Regards - Mr N. . . .

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

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

If Not Page.IsPostBack Then

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

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

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

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

"Bishoy George" <bi****@bishoy.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
你是对的。但是,请给我写一些关于缓存的更具体的编码:
1缓存代码:当我看到msdn时,设置有困难的东西,并且没有Cache.Add()的重载1个参数
正如你所写的那样。
2-究竟该放哪个缓存代码?
谢谢。

Mr Newbie <他** @ now.com>在消息中写道
新闻:uV ************** @ tk2msftngp13.phx.gbl ...
You are right. But, please write me more specific coding about caching:
1- code of caching exactly: as I looked at msdn there is difficult things
to set and there is no overloading of Cache.Add() that takes 1 parameter
as you wrote.
2- where exactly to put that caching code?
Thanks.

"Mr Newbie" <he**@now.com> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
问题是当你点击下拉菜单时Box,你触发一个
回发。不幸的是,您的Page_Load事件仅在初始GET期间填充数据集。因此,当您进行自动回溯时,没有数据可用。

一旦构建了Webform并呈现了页面,该对象就会被破坏。例如,您需要安排在初始
获取时填写数据并保存到缓存中。

缓存(" ds1)= Ds1

如果不是IsPostback则作为其他条件

ds1 = cache(" ds1")

page.DataBind().. 。 。 。PS:这是一个常见的错误。

HTH

Bishoy George <双**** @ bishoy.com>在消息中写道
新闻:uN **************** @ TK2MSFTNGP14.phx.gbl ...
The problem is that when you click the Dropdown Box, you trigger a
postback. Unfortunately, your Page_Load event only fills the dataset
during the initial GET. So there is no data to be had when you do your
Autopostback.

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

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

ds1 = cache("ds1")

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

PS: This is a common mistake to make.

HTH

"Bishoy George" <bi****@bishoy.com> wrote in message
news:uN****************@TK2MSFTNGP14.phx.gbl...
我有一个名为ds1的数据集填充来自Northwind数据库的2个表员工和客户
我有一个名为ddLastName的dropdownList,具有以下属性:

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

我有一个名为lblDisplay的标签

我希望标签在下拉列表中显示所选项目的Notes列数据。

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"方法= QUOT;交" 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.WebC ontrols.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");

d dLastName.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 f or Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ddLastName.SelectedIndexChanged += new
System.EventHandler(this.ddLastName_SelectedIndexC hanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ddLastName_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(ddLastName.SelectedIndex != 0)
{
int i = 0;
foreach(DataRow r in ds1.Tables["Employees"].Rows)
{
if(r["LastName"].ToString() == ddLastName.SelectedItem.Text)
{
lblDisplay.Text =
ds1.Tables["Employees"].Rows[i]["Notes"].ToString();
}
i++;
}
}
else
{
lblDisplay.Text = "";
}
}
}
}
I have a dataset called ds1 filled with 2 tables Employees and Customers
from Northwind database.
I have dropdownList called ddLastName with the following properties:

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

I have a label called lblDisplay

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

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

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

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

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

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

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

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

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

sda1.Fill(ds1,"Employees");

SqlDataAdapter sda2 = new SqlDataAdapter();

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

sda2.Fill(ds1,"Customers");

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

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

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

}
}

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

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

}
#endregion

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





这篇关于数据集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆