| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 991 人关注过本帖
标题:[求助]ADO.NET 对象哪里错了(也解决了)
只看楼主 加入收藏
beblue
Rank: 1
等 级:新手上路
帖 子:182
专家分:0
注 册:2007-3-24
收藏
 问题点数:0 回复次数:8 
[求助]ADO.NET 对象哪里错了(也解决了)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>通过DataTable和DataRow来访问DataSet的内容</title>
</head>
<script language="c#" runat="server" >
void page_load(object serder, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=127.0.0.1;uid=sa;pwd=;Initial Catalog=chapter");
DataSet ds = new DataSet();
DataTable dtable;
DataRowCollection coldrow;
DataRow drow;
int inti;
sqlcon.Open();
SqlDataAdapter sqld = new SqlDataAdapter("select * from student", sqlcon);
sqld.Fill(ds, "tabstudent");
dtable = ds.Tables["tastudent"];
coldrow = dtable.Rows;
for (inti=0;inti<coldrow.Count;inti++)
{
drow = coldrow[inti];
labContent.Text+= "学生号" + drow[0];
labContent.Text+= "姓名" + drow[1];
labContent.Text+= "年龄" + drow[2];
labContent.Text += "地址" + drow[3] + "<br/>";
drow[2] = Convert.ToInt32(drow[2]) + 1;

}
dtable.AcceptChanges();
dg.DataSource = ds.Tables["tabstudent"].DefaultView;
dg.DataBind();
sqlcon.Close();
sqlcon = null;
labContent.Text += "查找成功";


}

</script>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="dg" runat="server" /><br />
<asp:Label ID="labContent" runat="server" /><br />
</form>
</body>
</html>






未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。


我是vs2005+SQL2000
我实在找不出哪个对象出错了,大家帮我找找,,感激不尽,,明天就要交了

[此贴子已经被作者于2007-4-20 13:14:53编辑过]

搜索更多相关主题的帖子: ADO NET 对象 xhtml 
2007-04-20 02:40
beblue
Rank: 1
等 级:新手上路
帖 子:182
专家分:0
注 册:2007-3-24
收藏
得分:0 

设置了Debug="true" 后错误显示在
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:



行 22: coldrow = dtable.Rows;


可是还是不知乍改啊

[此贴子已经被作者于2007-4-20 3:09:49编辑过]


继续向前-------------------努力成为高手
2007-04-20 03:09
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
System.NullReferenceException
没有找到关联信息的异常,我没用过DataRowCollection,更不知道你定义的DataRow实例和DataRowCollection实例有什么意义,你可以再研究一下

我一般用
for (inti=0;inti<coldrow.Count;inti++)
{
labContent.Text+= "学生号" + dtable.Rows[inti][0];
labContent.Text+= "姓名" + dtable.Rows[inti][1];
labContent.Text+= "年龄" + dtable.Rows[inti][2];
labContent.Text += "地址" + dtable.Rows[inti][3] + "<br/>";
}

再有一个问题,楼主不知道在for里是可以定义变量的吗,就像下面的这样:
for(int i = 0; i < 10; i++)
{...}

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-04-20 09:04
川流不息
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2000
专家分:47
注 册:2006-11-8
收藏
得分:0 
coldrow = dtable.Rows;
呃,我也沒有用過這個DataRowCollection,我一般都是直接用daTable.Rows來直接操作的,不會把他放到一個集合中去。

日月更替,天地輪回,人間已是幾回春。 江山不老,人正少年,只手能擎半邊天。
2007-04-20 09:11
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 

.NET Framework 类库
DataRowCollection 类
表示 DataTable 的行的集合。

命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)

语法

Visual Basic(声明)
Public NotInheritable Class DataRowCollection
Inherits InternalDataCollectionBase

Visual Basic(用法)
Dim instance As DataRowCollection

C#
public sealed class DataRowCollection : InternalDataCollectionBase

C++
public ref class DataRowCollection sealed : public InternalDataCollectionBase

J#
public final class DataRowCollection extends InternalDataCollectionBase

JScript
public final class DataRowCollection extends InternalDataCollectionBase

备注

DataRowCollection 是 DataTable 的主要组件。当 DataColumnCollection 定义表的架构时,DataRowCollection 中包含表的实际数据,在该表中,DataRowCollection 中的每个 DataRow 表示单行。

您可通过调用 Add 和 Remove 方法,从 DataRowCollection 中插入和删除 DataRow 对象。您还可通过调用 Find 方法搜索在主键列中包含特定值的 DataRow 对象,也可通过调用 Contains 方法在基于字符的数据中搜索单个单词或词组。

示例

本节中的第一个示例打印 DataRowCollection 中每一行的第 1 列的值。第二个示例向 DataRowCollection 中添加用 NewRow 方法创建的新行。

Visual Basic 复制代码
Private Sub ShowRows(Byval table As DataTable)
' Print the number of rows in the collection.
Console.WriteLine(table.Rows.Count)

Dim row As DataRow
' Print the value of columns 1 in each row
For Each row In table.Rows
Console.WriteLine(row(1))
Next
End Sub

Private Sub AddRow(ByVal table As DataTable)
' Instantiate a new row using the NewRow method.
Dim newRow As DataRow = table.NewRow()
' Insert code to fill the row with values.

' Add the row to the DataRowCollection.
table.Rows.Add(newRow)
End Sub

C# 复制代码
private void ShowRows(DataTable table)
{
// Print the number of rows in the collection.
Console.WriteLine(table.Rows.Count);
// Print the value of columns 1 in each row
foreach(DataRow row in table.Rows)
{
Console.WriteLine(row[1]);
}
}

private void AddRow(DataTable table)
{
DataRowCollection rowCollection = table.Rows;
// Instantiate a new row using the NewRow method.

DataRow newRow = table.NewRow();
// Insert code to fill the row with values.

// Add the row to the DataRowCollection.
table.Rows.Add(newRow);
}

继承层次结构

System.Object
System.Data.InternalDataCollectionBase
System.Data.DataRowCollection


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-04-20 09:20
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 

查下msdn,知道DataRowCollection的用处了,也会用了,呵呵


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-04-20 09:23
川流不息
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2000
专家分:47
注 册:2006-11-8
收藏
得分:0 

暈:
sqld.Fill(ds, "tabstudent");
dtable = ds.Tables["tastudent"];
樓主你看看,你的表格名字錯了。


日月更替,天地輪回,人間已是幾回春。 江山不老,人正少年,只手能擎半邊天。
2007-04-20 09:25
川流不息
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2000
专家分:47
注 册:2006-11-8
收藏
得分:0 
沒錯,我剛也試了一下那個DataRowCollection,原來可以當作Rows的集合來用。不錯。

日月更替,天地輪回,人間已是幾回春。 江山不老,人正少年,只手能擎半邊天。
2007-04-20 09:25
beblue
Rank: 1
等 级:新手上路
帖 子:182
专家分:0
注 册:2007-3-24
收藏
得分:0 
以下是引用川流不息在2007-4-20 9:25:12的发言:

暈:
sqld.Fill(ds, "tabstudent");
dtable = ds.Tables["tastudent"];
樓主你看看,你的表格名字錯了。

哎,,就是这里的问题啊,,,谢谢各位

继续向前-------------------努力成为高手
2007-04-20 11:00
快速回复:[求助]ADO.NET 对象哪里错了(也解决了)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014992 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved