C# 为什么在设置行单元格的文本时会出现“System.ArgumentOutOfRangeException”?
使用大量查询,如下面的代码所示,我得到以下异常: 指定了“System.ArgumentOutOfRangeException”类型的异常 参数超出了有效值的范围 在这一行:C# 为什么在设置行单元格的文本时会出现“System.ArgumentOutOfRangeException”?,c#,mysql,asp.net,C#,Mysql,Asp.net,使用大量查询,如下面的代码所示,我得到以下异常: 指定了“System.ArgumentOutOfRangeException”类型的异常 参数超出了有效值的范围 在这一行: e.Row.Cells[3].Text = count; e.Row.Cells[3].Text = count; 有什么问题吗?我尝试了无数种不同的方法,但都无法奏效。我是这方面的新手 SqlConnection conn; conn = new SqlConnection("Data Source=.\\SQLEX
e.Row.Cells[3].Text = count;
e.Row.Cells[3].Text = count;
有什么问题吗?我尝试了无数种不同的方法,但都无法奏效。我是这方面的新手
SqlConnection conn;
conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("~\\App_Data\\ForumDB.mdf") + ";Integrated Security=True;User Instance=True");
conn.Open();
SqlCommand comm;
comm = new SqlCommand("SELECT COUNT(ThreadId) FROM [Threads] WHERE [TopicId] = @TopicId", conn);
SqlCommand comm2;
comm2 = new SqlCommand("SELECT MAX(PostedDate) FROM [Threads] WHERE [TopicId] = @TopicId", conn);
SqlCommand comm3;
comm3 = new SqlCommand("SELECT PostedBy FROM Threads WHERE PostedDate=(SELECT MAX(PostedDate) FROM [Threads] WHERE [TopicId] = @TopicId", conn);
//FOR COMMAND1 CMD
comm.Parameters.Add("@TopicId", System.Data.SqlDbType.Int, 10, "TopicId");
comm.Parameters["@TopicId"].Value = e.Row.Cells[0].Text;
string count = (comm.ExecuteScalar().ToString());
e.Row.Cells[3].Text = count;
//FOR COMMAND2 CMD1
comm2.Parameters.Add("@TopicId", System.Data.SqlDbType.Int, 10, "TopicId");
comm2.Parameters["@TopicId"].Value = e.Row.Cells[0].Text;
string count1 = (comm2.ExecuteScalar().ToString());
e.Row.Cells[4].Text = count1;
//for command3 cmd2
comm3.Parameters.Add("@TopicId", System.Data.SqlDbType.Int, 10, "TopicId");
comm3.Parameters["@TopicId"].Value = e.Row.Cells[0].Text;
if (comm3.ExecuteScalar() != null)
{
count2 = (comm3.ExecuteScalar().ToString());
}
conn.close();
这条线的问题是:
e.Row.Cells[3].Text = count;
e.Row.Cells[3].Text = count;
似乎此行没有4个单元格。通过在该行上放置断点并查看e.Row.Cells.Count的值来检查它。它将小于4
此外,您可能希望更改执行查询的方式,因为您似乎可以在一个查询中获得所需的所有字段,而不是三个单独的字段:
const string query =
"SELECT PostedBy, PostedDate, COUNT(ThreadId) AS Count " +
"FROM [Threads] WHERE [TopicId] = @TopicId " +
"ORDER BY PostedDate DESC " +
"LIMIT 1";
var fileToAttach = Server.MapPath("~\\App_Data\\ForumDB.mdf");
var connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + fileToAttach + ";Integrated Security=True;User Instance=True";
var topicId = int.Parse(e.Row.Cells[0].Text);
var postedBy = "";
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(query, conn)
{
cmd.Parameters.AddWithValue("@TopicId", topicId);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
// NOTE: I am assuming you have these cells now!
e.Row.Cells[3].Text = reader["Count"].ToString();
e.Row.Cells[4].Text = reader["PostedDate"].ToString();
postedBy = (string)reader["PostedBy"];
}
}
}
}
看起来您的e.行没有四个元素。您是否调试了代码并进行了检查?我似乎无法确定问题所在,我尝试更改了一些参数,但没有luck@rassii什么是e?你在哪里定义它?如何修改它?我99.9%确定行中的单元格少于4个。“e”的定义是什么?无论我将其更改为除0以外的任何值,都会引发相同的错误