Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

i have display sql data row in grid view colums but Sql query is not proper working.


Scoresheet is my sql table name.


What I have tried:

SELECT P_SC, P_LIT, TOT_WORK_P, MAIN_CL_P FROM Scoresheet

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
           this.GetData();
   }

   private void GetData()
   {
       var table = new DataTable();
       string connectionString = ConfigurationManager.ConnectionStrings["MediaMantradb"].ConnectionString;

       using (var connection = new SqlConnection(connectionString))
       {
           using (var command = new SqlCommand("SELECT P_SC, P_LIT, TOT_WORK_P, MAIN_CL_P FROM Scoresheet", connection))
           {
               using (var a = new SqlDataAdapter(command))
               {
                   connection.Open();
                   a.Fill(table);
                   connection.Close();
               }
           }
       }
       GridView1.DataSource = table;
       GridView1.DataBind();
   }
Posted
Updated 7-Dec-16 0:47am
v2
Comments
dbrenth 30-Nov-16 16:14pm    
What do you get when you run the SQL in the native SQL Server Management studio? It is possible you may need to qualify your table name. Make sure you log into Studio using the same user as in your connection string.

Hi,

Please check your connection string , is it proper ? after that make sure your connection done successfully ? Check SELECT P_SC, P_LIT, TOT_WORK_P, MAIN_CL_P FROM Scoresheet this is proper without any typo mistake.

Use Dataset like below.
C#
string connetionString = null;
SqlConnection connection ;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
connection = new SqlConnection(connetionString);
connection.Open();
adapter.SelectCommand = new SqlCommand("Your SQL Statement Here", connection);
adapter.Fill(ds);
connection.Close();
if(ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
 	  GridView1.DataSource = ds.Tables[0];
 	  GridView1.DataBind();
}
 
Share this answer
 
If you want the rows to be displayed as columns, then in that case you can use pivot to get this issue resolved.
Check this link:
Using PIVOT and UNPIVOT[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900