数据库中的数据绑定到GridView在ASP.Net [英] Binding database data to the GridView in ASP.Net

查看:90
本文介绍了数据库中的数据绑定到GridView在ASP.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试数据库中的数据绑定到C#和asp.net GridView控件。但我无法看到gridview.Rows的DATAS被添加到GridView但都是空的。当我运行SQLServer的在该查询,它提供了正确的result.I没有添加或更改任何code到ASP part.Should我?我找不到哪里出了问题:(请帮助..

  MyConnection的= WebConfigurationManager.ConnectionStrings [KutuphaneConnectionString]的ConnectionString。
连接=新的SqlConnection(MyConnection的);
命令=新的SqlCommand();
connect.Open();
command.Connection =连接; 字符串komut =SELECT K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi FROM OduncIslemleri 0,Kitap K式O.kullaniciId =+会话[ID] +和O.kitapId = K.id;
        尝试
        {
            的SqlCommand的SqlCommand =新的SqlCommand();                的SqlCommand = connect.CreateCommand();
                sqlCommand.CommandText = komut;
                SqlDataAdapter的SDA =新SqlDataAdapter的(sqlCommand.CommandText,连接);
                SqlCommandBuilder SCB =新SqlCommandBuilder(SDA);
                //创建一个DataTable来保存查询结果。
                数据表dTable =新的DataTable();
                //填写的数据表。
                sda.Fill(dTable);
                GridView1.DataSource = dTable;
                GridView1.DataBind();        }
        赶上(SQLEXCEPTION)
        {
            //Console.WriteLine(e.StackTrace);
        }reader.Close();
connect.Close();


解决方案

下面是正确答案:

  MyConnection的= WebConfigurationManager.ConnectionStrings [KutuphaneConnectionString]的ConnectionString。
连接=新的SqlConnection(MyConnection的);
字符串sorgu =选择K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi从Kitap K,OduncIslemleri O,其中O.kitapId = K.id和O.kullaniciId =+会话[ID];
SqlDataAdapter的SADP =新SqlDataAdapter的(sorgu,连接);
DataSet的DS =新的DataSet();
sadp.Fill(DS);
this.GridView1.DataSource = ds.Tables [0];
this.GridView1.DataBind();
connect.Close();

我也用在GridView的模板字段。也autogeneratedFields应该是真实的。我希望这有助于谁有同样的问题的人。

I try to bind database data to the gridview in c# and asp.net. But I couldn't see the datas in the gridview.Rows are added to the gridview but they are empty. When I run that query in SQLServer, it gives the correct result.I didn't add or change any code to the asp part.Should I? I couldn't find where is the problem :( please help..

myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
command = new SqlCommand();
connect.Open();
command.Connection = connect;

 string komut = "SELECT K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi FROM OduncIslemleri O,Kitap K WHERE O.kullaniciId=" + Session["id"] + " AND O.kitapId = K.id;";
        try
        {
            SqlCommand sqlCommand = new SqlCommand();

                sqlCommand = connect.CreateCommand();
                sqlCommand.CommandText = komut;
                SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connect);
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                //Create a DataTable to hold the query results.
                DataTable dTable = new DataTable();
                //Fill the DataTable.
                sda.Fill(dTable);
                GridView1.DataSource = dTable;
                GridView1.DataBind();

        }
        catch (SqlException)
        {
            //Console.WriteLine(e.StackTrace);
        }

reader.Close();
connect.Close();

解决方案

Here is the correct answer :

myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
string sorgu = "select K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi from Kitap K, OduncIslemleri O where O.kitapId = K.id and O.kullaniciId = "+ Session["id"];
SqlDataAdapter sadp = new SqlDataAdapter(sorgu, connect);
DataSet ds = new DataSet();
sadp.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
connect.Close();

I also used template fields in Gridview. Also autogeneratedFields should be true. I hope this helps to the people who have the same problem

这篇关于数据库中的数据绑定到GridView在ASP.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆