0

I am having problems getting data to be displayed in a gridview in VS 2010. I have a SQLdataSource which is connecting to my database but the gridview is not showing at all, my error message is all that is displayed. Would anyone know why this is?

This is the code that I have:

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Services.Description;

namespace DBprototype2
{
    public partial class _Default : System.Web.UI.Page
    {
             void Page_Load(Object sender, EventArgs e)
  {

    // This example uses Microsoft SQL Server and connects
    // to the Northwind sample database. The data source needs
    // to be bound to the GridView control only when the 
    // page is first loaded. Thereafter, the values are
    // stored in view state.                      
    if(!IsPostBack)
    {

      // Declare the query string.
      String queryString = 
        "SELECT * FROM ";

      // Run the query and bind the resulting DataSet
      // to the GridView control.
      DataSet ds = GetData(queryString);
      if (ds.Tables.Count > 0)
      {
        GridView1.DataSource = ds;
        GridView1.DataBind();
        Label1.Text = "Connected.";
      }
      else
      {
        Label1.Text = "Unable to connect to the database.";
      }

    }     

  }

  DataSet GetData(String queryString)
  {

    // Retrieve the connection string stored in the Web.config file.
      String connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

      DataSet ds = new DataSet();

    try
    {
      // Connect to the database and run the query.
      SqlConnection connection = new SqlConnection(connectionString);        
      SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

      // Fill the DataSet.
      adapter.Fill(ds);
      Label1.Text = "Connected.";

    }
    catch(Exception ex)
    {

      // The connection failed. Display an error message.
      Label1.Text = "Unable to connect.";


    }

    return ds;

  }
             }
        }




`

If anyone could tell me the problem with my code that would be extremely helpful.

2
  • You should check and confirm your connection string is correct . Commented Dec 2, 2013 at 15:23
  • BTW show us your gridview code as well but before that check your Dataset ds if it is not null or rowcount is greater than 0 in GetData method then check your gridview code else check your ConnectionString. Commented Dec 2, 2013 at 15:27

1 Answer 1

2

The problem is your query:

String queryString = 
        "SELECT * FROM ";

You are missing the table to select from, later in your method GetData you are not adding any table name in the query, that is why it is failing.

  • Also consider enclosing your connection in using statement
  • Use the Exception object ex.Message instead of hard coded error in label, this will help you debug and see what exactly is going wrong.
  • Not for this particular scenario but always consider using parametrized queries.
Sign up to request clarification or add additional context in comments.

2 Comments

@user2674605, just set your query like SELECT * FROM someTable where someTable is the name of your table, and don't modify GetData
Thanks for the help, but I updated this and it still gives me the same error message?

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.