| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1085 人关注过本帖
标题:麻烦大家帮我看一下这个,列名无效是怎么回事??
只看楼主 加入收藏
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
结帖率:89.47%
收藏
已结贴  问题点数:20 回复次数:7 
麻烦大家帮我看一下这个,列名无效是怎么回事??
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            RadioButtonList1.DataSource = GetDs(1);
            RadioButtonList1.DataTextField = "ItemName";
            RadioButtonList1.DataValueField = "id";
            RadioButtonList1.DataBind();
            RadioButtonList2.DataSource = GetDs(2);
            RadioButtonList2.DataTextField = "ItemName";
            RadioButtonList2.DataBind();
            CheckBoxList1.DataSource = GetDs(1);
            CheckBoxList1.DataTextField = "ItemName";
            CheckBoxList1.DataBind();
            
        }

    }
    /// <summary>
    /// 返回制定parent的ds
    /// </summary>
    /// <param name="parentid"></param>
    /// <returns></returns>
    protected DataSet GetDs(int parentid)
    {
        SqlConnection conn = new SqlConnection("Data Source=PC-201009101252;Initial catalog=Vote;Persist Security Info=True;User ID=sa;Password=123;");//建立与数据库连接
        string strSql = string.Format("select * from Item where parentid={0}",parentid);
        SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "Item");
        return ds;
        
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int id = 0;
        try
        {
            id = Convert.ToInt32(RadioButtonList1.SelectedValue);
        }
        catch
        {
            id = 0;
        }
        SqlConnection conn = new SqlConnection("Data Source=PC-201009101252;Initial catalog=Vote;Persist Security Info=True;User ID=sa;Password=123;");//建立与数据库连接
        string strSql = string.Format("update Item set VoteName=VoteName+1 where id={0}", id);
        SqlCommand cmd = new SqlCommand(strSql, conn);
        conn.Open();
        int i = cmd.ExecuteNonQuery();
        conn.Close();
        if (i > 0)
        {
            YYCMS.Jscript.AlertAndRedirect("投票成功!", "VoteList.aspx");
        }

        else
        {
            YYCMS.Jscript.AlertAndRedirect("投票失败!", "VoteList.aspx");
        }

    }
    protected void  Button2_Click(object sender, EventArgs e)
{
       int id = 0;
        try
        {
            id = Convert.ToInt32(RadioButtonList2.SelectedValue);
        }
        catch
        {
            id = 0;
        }
        SqlConnection conn = new SqlConnection("Data Source=PC-201009101252;Initial catalog=Vote;Persist Security Info=True;User ID=sa;Password=123;");//建立与数据库连接
        string strSql = string.Format("update Item set VoteName=VoteName+1 where id={0}", id);
        SqlCommand cmd = new SqlCommand(strSql, conn);
        conn.Open();
        int i = cmd.ExecuteNonQuery();
        conn.Close();
        if (i > 0)
        {
            YYCMS.Jscript.AlertAndRedirect("投票成功!", "VoteList.aspx");
        }

        else
        {
            YYCMS.Jscript.AlertAndRedirect("投票失败!", "VoteList.aspx");
        }


}
    protected void Button3_Click(object sender, EventArgs e)
    {
        string idstr = "0";
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                idstr += "," + li.Value;
            }
        }
        idstr += ",0";
        SqlConnection conn = new SqlConnection("Data Source=PC-201009101252;Initial catalog=Vote;Persist Security Info=True;User ID=sa;Password=123;");//建立与数据库连接
        string strSql = string.Format("update Item set VoteName=VoteName+1 where id in({0})", idstr);
        SqlCommand cmd = new SqlCommand(strSql, conn);
        conn.Open();
        int i = cmd.ExecuteNonQuery();
        conn.Close();
        if (i > 0)
        {
            YYCMS.Jscript.AlertAndRedirect("投票成功!", "VoteList.aspx");
        }

        else
        {
            YYCMS.Jscript.AlertAndRedirect("投票失败!", "VoteList.aspx");
        }


    }
}
搜索更多相关主题的帖子: 麻烦 列名 
2010-09-13 21:00
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-09-13 21:04
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
上面是运行时候出错提示
2010-09-13 21:05
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:5 
应该是sql有问题
复制其sql到mssql中测试下就知咯.

C#超级群 74862681,欢迎大家的到来!
2010-09-13 21:59
红色警戒
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:19
帖 子:444
专家分:2967
注 册:2005-11-20
收藏
得分:5 
VoteName是什么类型?

2010-09-13 22:17
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
是int型,我本来想写个VoteNUM 写错了写成Votename
2010-09-13 22:33
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:5 
像这种情况最好用try...catch(exception ex)...测试一下,有问题就把问题的message显示出来,这样对调试很有帮助。

出现这种错误,我估计有可能可能和数据打开有一定关系,你最好先判断一下数据库是否打开了,然后再执行int i = cmd.ExecuteNonQuery();
在这句话前面加上判断 if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();//打开数据库连接
                    if (conn.State == ConnectionState.Open)
                    {
                      int i = cmd.ExecuteNonQuery();
当然程序没有写全,这里只是一个提示,仅供参考!!
2010-09-14 21:56
红色警戒
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:19
帖 子:444
专家分:2967
注 册:2005-11-20
收藏
得分:5 
调试运行看看string strSql = string.Format("update Item set VoteName=VoteName+1 where id in({0})", idstr);
这条语句的具体值

2010-09-14 22:32
快速回复:麻烦大家帮我看一下这个,列名无效是怎么回事??
数据加载中...
 
   



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

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