3

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();
3
  • 2
    You should provide the aspx-part of the GridView. Are you AutoGenerateColumns? Besides, remember not to rebind the GridView from Page_Load in Postbacks. So always check for !Page.IsPostback before you DataBind the Grid. Commented May 13, 2011 at 23:15
  • how? could you explain in code samples? Commented May 13, 2011 at 23:25
  • first complete with the missing aspx-part in your question. And what part would you like to be explained? The GridView-property AutoGenerateColumns is selfexplaining, besides i've provided a link. The postback-check is also simple. Commented May 13, 2011 at 23:31

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

watch for another event triggered after the bind that could be clearing the rows

1 Comment

I don't think so.. I wrote it page_load class and they are te last sentences than page is shown.
0

Try creating a DataSet and populate that using Fill instead. I've never seen Fill used on a DataTable - and can't find that particular overload on MSDN. My suspicion is, though, that such an overload would not modify the existing schema of the DataTable (which, since it's only just been created prior to use in your example, would mean that it has no columns).

2 Comments

I tried this but it gave also same result : 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;"; SqlCommand cmd = new SqlCommand(komut, connect); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Kitap"); GridView1.DataSource = ds; GridView1.DataBind();
How about with GridView1.DataSource = ds.Tables(0); ?
0

I think you have to use a BindingSource Control, you set the DataSource of it to the DataTable, and then set the GridView's DataSource to the BindingSource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.