郁闷

xiaoming851 2003-07-25 09:55:21
private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(!i)
{
e.Item.Cells[3].Text = "测试";
}
else
{
string CommandText = "select count(*) as count from zrbhdetail where parent=" + e.Item.Cells[0].Text;
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
SqlCommand Command = new SqlCommand(CommandText, myConnection);
myConnection.Open();
int total = (int)Command.ExecuteScalar();
myConnection.Close();
// return total.ToString();
e.Item.Cells[3].Text = total.ToString();
// e.Item.Cells[3].Text = GetCount(e.Item.Cells[0].Text);
}
i=true;
}


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

异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '&' 附近有语法错误。

源错误:


行 87: SqlCommand Command = new SqlCommand(CommandText, myConnection);
行 88: myConnection.Open();
行 89: int total = (int)Command.ExecuteScalar();
行 90: myConnection.Close();
行 91: // return total.ToString();

...全文
38 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwbyxw 2003-07-25
  • 打赏
  • 举报
回复
还没解决吗,按zjjszw(阿丸) 说的

string CommandText = "select count(*) as count from zrbhdetail where parent=" + e.Item.Cells[0].Text;
改成
string CommandText = "select count(*) as count from zrbhdetail where parent='" + e.Item.Cells[0].Text + "'";
应该可以了。
CMIC 2003-07-25
  • 打赏
  • 举报
回复
使用SQL事件探查器,看看提交的SQL语句到底是什么
river168 2003-07-25
  • 打赏
  • 举报
回复
sql语句出错, 你先输出SQL语句就知道错的结果了.
blackcatiii 2003-07-25
  • 打赏
  • 举报
回复
datagrid的表头部分也会在e.Item里出现,所以要把这部分去掉。建议你输出的时候将输出结果各部分用分隔符分开,否则也许有个内容是空的但显示却看不到。
declude 2003-07-25
  • 打赏
  • 举报
回复
string CommandText = "select count(*) as count from zrbhdetail where parent="+Convert.ToInt16(e.item.cells[0].text);
xiaoming851 2003-07-25
  • 打赏
  • 举报
回复
to blackcatiii(ljh):
不好意思,最后我还是按照你的方法试了,并且成功了!
可我更加郁闷了
为什么我遍历输出e.Item.Cells[0].Text的时候,都有值,但传递或者调用此值的时候却没有值哪???
好奇怪,难道他遍历次数要大于数据的条数?!?
更加郁闷啊~~~~
xiaoming851 2003-07-25
  • 打赏
  • 举报
回复
To blackcatiii:
可是我还专门将哪些e.Item.Cells[0].Text循环输出,都看到值了,好郁闷啊~~
  • 打赏
  • 举报
回复
gz
xiaoming851 2003-07-25
  • 打赏
  • 举报
回复
To declude
这样怎么会行那!
如果我改为:
string GetCount(String strtag)
{
string CommandText = "select count(*) as count from zrbhdetail where parent=" + strtag;

SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
SqlCommand Command = new SqlCommand(CommandText, myConnection);
myConnection.Open();
int total = (int)Command.ExecuteScalar();
myConnection.Close();
return total.ToString();
}

private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(!i)
{
e.Item.Cells[3].Text = "测试";
}
else
{
e.Item.Cells[3].Text = GetCount(e.Item.Cells[0].Text);
}
i=true;
}
其实是一样的,问题依然存在,好伤啊~~~~
哪位大哥帮帮小弟?
blackcatiii 2003-07-25
  • 打赏
  • 举报
回复
估计是e.Item.Cells[0].Text没有值的原因。
加个判断
if (e.Item.ItemIndex >= 0)
{
if(!i)
{
//...
}
else
{
//...
}
}
declude 2003-07-25
  • 打赏
  • 举报
回复
parent是整数类型?
如果是改为:
Convert.ToInt16(e.item.cells[0].text);
或者Int16.Parse(e.item.cells[0].text)
zjjszw 2003-07-25
  • 打赏
  • 举报
回复
string CommandText = "select count(*) as count from zrbhdetail where parent='" + e.Item.Cells[0].Text+"'";
xiaoming851 2003-07-25
  • 打赏
  • 举报
回复
to cpp2017:
大哥啊~,SQL里边的字段是数值型啊~
伤啊~
blackcatiii 2003-07-25
  • 打赏
  • 举报
回复
估计是e.Item.Cells[0].Text没有值的原因。
加个判断
if (e.Item.ItemIndex >= 0)
{
if(!i)
{
//...
}
else
{
//...
}
}
cpp2017 2003-07-25
  • 打赏
  • 举报
回复

string CommandText = "select count(*) as count from zrbhdetail where parent='" + e.Item.Cells[0].Text+"'";

xiaoming851 2003-07-25
  • 打赏
  • 举报
回复
to river168
根本没有,e.item.cells[0].text是数字我将其转换为字符串
并且我遍历输出e.item.cells[0].text这个,都是数字啊
"select count(*) as count from zrbhdetail where parent=8"
如果改成这样的方法,就没任何问题,好郁闷啊~~~~
alf7927 2003-07-25
  • 打赏
  • 举报
回复
uop
goody9807 2003-07-25
  • 打赏
  • 举报
回复
up
river168 2003-07-25
  • 打赏
  • 举报
回复
string CommandText = "select count(*) as count from zrbhdetail where parent=" + e.Item.Cells[0].Text;

e.Item.Cells[0].Text 这个值里面含有字符'&", 你看看是不是?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧