Reputation: 431
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.
Upvotes: 2
Views: 7059
Reputation: 754200
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?
Upvotes: 1
Reputation: 3880
SqlConnection connect = new SqlConnection(conn);
connect.Open();
Upvotes: 0