注册 登录
编程论坛 ASP.NET技术论坛

[求助]ADO.NET 对象哪里错了(也解决了)

beblue 发布于 2007-04-20 02:40, 995 次点击

<%@ 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编辑过]

8 回复
#2
beblue2007-04-20 03:09

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

源错误:



行 22: coldrow = dtable.Rows;


可是还是不知乍改啊

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

#3
冰镇柠檬汁儿2007-04-20 09:04
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++)
{...}
#4
川流不息2007-04-20 09:11
coldrow = dtable.Rows;
呃,我也沒有用過這個DataRowCollection,我一般都是直接用daTable.Rows來直接操作的,不會把他放到一個集合中去。
#5
冰镇柠檬汁儿2007-04-20 09:20

.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

#6
冰镇柠檬汁儿2007-04-20 09:23

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

#7
川流不息2007-04-20 09:25

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

#8
川流不息2007-04-20 09:25
沒錯,我剛也試了一下那個DataRowCollection,原來可以當作Rows的集合來用。不錯。
#9
beblue2007-04-20 11:00
以下是引用川流不息在2007-4-20 9:25:12的发言:

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

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