且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据库中的数据绑定到GridView在ASP.Net

更新时间:2022-10-21 09:01:41

下面是正确答案:

  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