编程论坛's Archiver

雪夜白狼 发表于 2008-5-9 16:52

多个datalist的绑定问题————?

我做了个自定义控件里面有多个datalist需要数据绑定
我是这样写的可是出现了错误

[color=Red]public partial class Controllers_first : System.Web.UI.UserControl
{
    OleDbConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = DB.DBconntion();
        conn.Open();

        string sql = "select top 8 * from news where type='IT业界'";
        
        
        

        DB dt = new DB();

        conn.Close();

        DataList1.DataSource=dt.DBtable(sql).Tables[0].DefaultView;
        DataList1.DataBind();


        conn = DB.DBconntion();
        conn.Open();
        string sql1 = "selecttop 8 * from news where type='PS专区'";


        DB dt1 = new DB();

        conn.Close();

        DataList2.DataSource = dt1.DBtable(sql1).Tables[0].DefaultView;
        DataList2.DataBind();


        conn = DB.DBconntion();
        conn.Open();
        string sql2 = "select top 8 * from news where type='硬件天下'";

        DB dt2 = new DB();

        conn.Close();

        DataList3.DataSource = dt2.DBtable(sql2).Tables[0].DefaultView;
        DataList3.DataBind();

        conn = DB.DBconntion();
        conn.Open();
        string sql3 = "select top 8 * from news where type='FLASH专区'";


        DB dt3 = new DB();
        conn.Close();

        DataList4.DataSource = dt3.DBtable(sql3).Tables[0].DefaultView;
        DataList4.DataBind();[/color]
        




    我想可能是sql语句不能这样重复出现吧


错误::
[color=Red]无效的 SQL语句;期待 'DELETE'、'INSERT'、'PROCEDURE'、'SELECT'、或 'UPDATE'。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.OleDb.OleDbException: 无效的 SQL语句;期待 'DELETE'、'INSERT'、'PROCEDURE'、'SELECT'、或 'UPDATE'。[/color]


要怎么写呢?

hebingbing 发表于 2008-5-9 17:08

不管程序有没有错误,首先感觉你的程序写的太……
      
       //conn = DB.DBconntion();
        //conn.Open();

        string sql = "select top 8 * from news where type='IT业界'";
        
        
        

        DB dt = new DB();

       // conn.Close();   我想这里的conn是不必要的吧?你用到conn了吗?

        DataList1.DataSource=dt.DBtable(sql).Tables[0].DefaultView;
        DataList1.DataBind();


        //conn = DB.DBconntion();
        //conn.Open();
        string sql1 = "selecttop 8 * from news where type='PS专区'";


      //  DB dt1 = new DB();

        //conn.Close();

        DataList2.DataSource = dt.DBtable(sql1).Tables[0].DefaultView;
        DataList2.DataBind();


      //  conn = DB.DBconntion();
        //conn.Open();
        string sql2 = "select top 8 * from news where type='硬件天下'";

       // DB dt2 = new DB();

       // conn.Close();

        DataList3.DataSource = dt.DBtable(sql2).Tables[0].DefaultView;
        DataList3.DataBind();

        //conn = DB.DBconntion();
        //conn.Open();
        string sql3 = "select top 8 * from news where type='FLASH专区'";


      //  DB dt3 = new DB();
       // conn.Close();

        DataList4.DataSource = dt.DBtable(sql3).Tables[0].DefaultView;
        DataList4.DataBind();

雪夜白狼 发表于 2008-5-9 17:31

回复 2# 的帖子

呵呵
开始的时候我也是你那样写的啊
我也认为如果像上面的写法太啰嗦了

可是出现的错时说
在此范围内定义了局部变量bt我就加上了那写东西
这样写
还是不行

雪夜白狼 发表于 2008-5-9 17:42

over了啊
谢谢你啊
看来我以后要细心点了啊o
多动脑子,少发帖子了啊!!

snipen 发表于 2008-5-9 19:58

你的连接都已经关了,下面的东西就没有办法连接了,我想应该到最后再关吧,

hebingbing 发表于 2008-5-9 20:18

更不就不用开数据库连接……
根本就没有用到

snipen 发表于 2008-5-9 20:39

...

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.