2

I have a question. I've tried to solve it all day long and I am really stuck. I'm using VS2010 and SQL Server 2012 (quite a bad combination) and I'm trying to execute a fairly simple piece of C# and ASP.net code which goes like this:

string conn = ConfigurationManager.ConnectionStrings["BazaConnectionString"].ConnectionString;

SqlConnection connect = new SqlConnection(conn);

SqlDataAdapter sqlAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("SELECT * FROM  Seminar", connect);
sqlAdapter.SelectCommand = cmd;

DataTable tablica = new DataTable();
sqlAdapter.Fill(tablica);

GridView1.DataSource = tablica;
GridView1.DataBind();

The problem is my gridview is always empty. I have data in the table and SELECT * should select all of it but I get an empty table returned. I've been trying Dataset and DataTable but nothing seems to work. Any help is appreciated. Thanks in advance.

1
  • 1
    And you're sure that (1) your connection string is correct, (2) your SELECT statement really returns values, and (3) you're not getting any exception in the process that you're just swallowing and ignoring?? Commented Sep 23, 2012 at 14:45

2 Answers 2

1

I have a hunch that you might be running into an exception - possibly a timeout - and you're not dealing with this properly...

Try something like this:

string conn = ConfigurationManager.ConnectionStrings["BazaConnectionString"].ConnectionString;

using (SqlConnection connect = new SqlConnection(conn))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM  Seminar", connect))
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd))
{
   try
   {
      DataTable tablica = new DataTable();
      sqlAdapter.Fill(tablica);

      GridView1.DataSource = tablica;
      GridView1.DataBind();
   }
   catch(Exception exc)
   {
       string msg = exc.GetType().FullName + ": " + exc.Message;
   }
}

If you execute this code - do you happen to fall into the catch block? If so: what's the exception? What does it tell you?

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

3 Comments

No exceptions :S The connection string is okay. It connects to correct table. If i put for example name of a column that is in the table it displays nothing, but if i put name of non existing column i get error that column isn't there so it seems to be looking the correct DB and Table, but why it sees nothing in it, that is what i don't know :S
@user1692436: and if you do a int rowCount = tablica.Rows.Count; right after filling the DataTable - do you have rows in the resulting DataTable? If yes: then there's something wrong with your grid, most likely (does it have fixed column definitions that might not be matching any of the columns returned in your DataTable?)
The problem seems to be the gridview my row count was 4, and now i deleted the gridview and put in a new one and it works indeed. before i put drag and drop datasource and the grid view seems to have taken some settings from there. Thank you a lot.
0
SqlConnection connect = new SqlConnection(conn);
connect.Open();

1 Comment

No, that's not needed - the SqlDataAdapter will open (and close) the connection as needed.

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.