你的“System.NullReferenceException: 未将对象引用设置到对象的实例。
”是在那里出现的?出现这样信息主要是由于没有将数据原帮定到你的控件上
你仔细检查一下你的数据原是否正确?
”是在那里出现的?出现这样信息主要是由于没有将数据原帮定到你的控件上
你仔细检查一下你的数据原是否正确?
解决方案 »
- 大家进来帮忙看看
- 如何读取Excel表格中的内容
- 那篇关于在线用户列表文章中的Visitor.Current.IsGuest在ASP.NET中究竟是什么对象名?
- 百分求教
- ASP.NET调用DLL的问题!(在线急等!!!!来者有分!!!)
- Chinese input error. HOW to let the invisualable columns become visualable . How to coding it in the .cs file.
- 请问DataTable用编程方法怎么设置单元格的对齐方式为居中?多谢
- 怪:这个网站怎么看不到完整的源文件html代码(http://7m.cn/ -》即使比分-》最新赛果
- 如何利用程序禁止IE弹出下载对话框?在线等!
- 点击一个按钮后,向asp.net页面指定位置动态加html和各种控件的方法有多少?讨论讨论。。
- 在C#操作word,怎么查找替换,怎么没有找到相应的类和方法
- 关于Application的生命期(急!急!!)
可以直接使用TextBox1.Text = DropDownList1.SelectedItem.Text
或TextBox1.Text = DropDownList1.SelectedItem.value
int NowRecord=DropDownList1.SelectedIndex;
DataRow NowRow = sqldataset.Tables["用户表"].Rows[NowRecord];
Label1.Text=NowRow["用户名"].ToString();
建议使用PreRender事件,即可
Session["mydataset"] = sqldataset;
然后在你的代码中如下使用:
DataRow NowRow = ((DataSet)Session["mydataset"]).Tables["用户表"].Rows[NowRecord];
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection sqlconn=new SqlConnection("server=zsb;database=zsb;uid=sa;pwd=1234;");
sqlconn.Open();
SqlDataAdapter sqladapter=new SqlDataAdapter("select * from 用户表",sqlconn);
sqladapter.Fill(sqldataset,"sqladapter");
DataGrid1.DataSource=sqldataset.Tables["sqladapter"].DefaultView;
DataGrid1.DataBind();
DropDownList1.DataSource=sqldataset.Table["sqladapter"].DefaultView;
DropDownList1.DataTextField="用户名";
DropDownList1.DataBind();
// Put user code to initialize the page here
}难道我在SelectedIndexChanged事件中还要设置它的数据源吗?关键是SelectedIndexChanged事件没有反应啊?????
dropdownlist的autopost设为true,没有取到数据
Label1.Text=DropDownList1.SelectedItem.Text;
<%@ Import namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>
<html>
<head>
<script language="C#" runat="server"> void MyListChangeCode(Object objSender,EventArgs objArgs)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Value + ".";
}
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1" runat="server"
OnSelectedIndexChanged="MyListChangeCode" AutoPostBack="true">
<asp:ListItem Text="Option 1" Value="Value 1" />
<asp:ListItem Text="Option 2" Value="Value 2" />
<asp:ListItem Text="Option 3" Value="Value 3" />
</asp:DropDownList>
<br>
<asp:Button id="Button1"
Text="Submit"
runat="server"/>
<br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
取道的总是第一条值???
我把它写成:
int NowRecord=DropDownList1.SelectedIndex;
DataRow NowRow =((DataSet)Session["mydataset"]).Tables["用户表"].Rows[NowRecord];
Label1.Text=NowRow["用户名"].ToString();Session["mydataset"]是在Page_Load中赋值的,但运行时还是说
DataRow NowRow =((DataSet)Session["mydataset"]).Tables["用户表"].Rows[NowRecord];
有错:System.NullReferenceException: 未将对象引用设置到对象的实例。我该怎么做???
出错都指到:
DataRow NowRow =((DataSet)Session["mydataset"]).Tables["用户表"].Rows[NowRecord];错误信息:System.NullReferenceException: 未将对象引用设置到对象的实例。如果用Label1.Text=DropDownList1.SelectedItem.Text;
就老是取到第一条值??是否是编译器的版本问题???
你可以将DropDownList的value绑定为用户名Label1.Text=DropDownList1.SelectedItem.Value;
不就成了吗。。
dropdownlist有设置autopostback=true吗!
当然也是熟悉控件用法了。
{
SqlConnection sqlconn=new SqlConnection"server=zsb;database=zsb;uid=sa;pwd=1234;");
sqlconn.Open();
SqlDataAdapter sqladapter=new SqlDataAdapter("select * from 用户表 ",sqlconn);
DataSet sqldataset=new DataSet();
sqladapter.Fill(sqldataset,"sqladapter");
Session["mydataset"] = sqldataset;
DataGrid1.DataSource=sqldataset.Tables["sqladapter"].DefaultView;
DataGrid1.DataBind();Label2.Text=sqldataset.Tables["sqladapter"].Rows[4]["区县名称"].ToString(); DropDownList1.DataSource=sqldataset.Tables["sqladapter"].DefaultView;
DropDownList1.DataTextField="用户名";
DropDownList1.DataBind();
sqldataset.Dispose();
sqlconn.Close();
// Put user code to initialize the page here
}SelectedIndexChanged如下:private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{DataSet NowDS = (DataSet)Session["mydataset"];
int NowRecord=DropDownList1.SelectedIndex;
DataRow NowRow =NowDS.Tables["用户表"].Rows[NowRecord];
Label1.Text=NowRow["用户名"].ToString();}
---》
SqlConnection("server=zsb;database=zsb;uid=sa;pwd=1234;");
括号!
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{DataSet NowDS = (DataSet)Session["mydataset"];
int NowRecord=DropDownList1.SelectedIndex;
DataRow NowRow =NowDS.Tables["用户表"].Rows[NowRecord];
Label1.Text=NowRow["用户名"].ToString();}
无法处发的问题;
请先把缘来的代码注释掉。
在aspx的页面双击dropdownlist,代码回自动生成。
再用鼠标右击你的工程,选择debugging,选中asp.net和asp debug。
please trying。
page_load:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Validate initially to force asterisks
// to appear before the first roundtrip.
if (!IsPostBack)
{
// Label1.Text="ht";
SqlConnection sqlconn=new SqlConnection("server=zsb;database=zsb;uid=sa;pwd=1234;");
sqlconn.Open();
SqlDataAdapter sqladapter=new SqlDataAdapter("select * from 用户表 ",sqlconn);
DataSet sqldataset=new DataSet();
sqladapter.Fill(sqldataset,"sqladapter");
Session["mydataset"] = sqldataset;
sqldataset.Dispose();
sqlconn.Close(); DataGrid1.DataSource=Session["mydataset"].Tables["sqladapter"].DefaultView;
DataGrid1.DataBind(); Label2.Text=Session["mydataset"].Tables["sqladapter"].Rows[4]["区县名称"].ToString(); DropDownList1.DataSource=Session["mydataset"].Tables["sqladapter"].DefaultView;
DropDownList1.DataTextField="用户名";
DropDownList1.DataBind();
}
}