Associating Database Data with GridView in ASP.Net

I am trying to bind database data to gridview in C # and asp.net. But I could not see the data in gridview.Rows are added to gridview, but they are empty. When I run this query in SQLServer, it gives the correct result. I did not add or change any code in asp part.Should I? I could not find where the problem is: (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();
+3
source share
4 answers

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 auto-generated fields must be true. I hope this helps people who have the same problem.

+4
source

watch for another event raised after binding that might clear the lines

0
source

Try creating a DataSet and fill it with Fill. I have never seen the Fill used in a DataTable, and cannot find this overload on MSDN. My suspicion, however, is that such overloading will not change the existing DataTable schema (which, since it was just created before use in your example, would mean that it has no columns).

0
source

I think you need to use the BindingSource Control, you set the DataSource for it to a DataTable, and then set the DataSource GridView to BindingSource.

0
source

All Articles