简体   繁体   中英

How to populate grid view from an SQL table

I am trying to populate a grid view from a query I have in an SQL table through a connection string yet somehow I receive the following error. Realistically as I am a beginner I think this is going to be an extremely simple error with the code so don't assume anything!

"The system cannot find the file specified

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified"

The following is my code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;

namespace GridviewTOExcel
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.; database=Richard2016DB; integrated security=SSPI");
            SqlCommand cmd = new SqlCommand("Select * from dbo.tblEmployee", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;
            GridView1.DataBind();
            con.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        }
    }
}

I believe you should send it to a DataTable first before being able to bind it.

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("data source=.; database=Richard2016DB; integrated security=SSPI");
    SqlCommand cmd = new SqlCommand("Select * from dbo.tblEmployee", con);
    con.Open();
    DataTable table = New DataTable();
    using(SqlDataAdapter da = new SqlDataAdapter(cmd))
        da.Fill(table);
    GridView1.DataSource = table;
    GridView1.DataBind();
    con.Close();
}

You will need the following reference to use DataTable's

using System.Data;
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
    namespace GridviewTOExcel
    {
      public partial class WebForm1 : System.Web.UI.Page
      {
        private SqlConnection con;
        private SqlCommand cmd;
        private string constr, query;

        private void connection()
        {
            string dbConnectiomName = "conStr"; // Set your keyname from web.config file
            constr = WebConfigurationManager.ConnectionStrings[dbConnectiomName].ToString();
            con = new SqlConnection(constr);
            con.Open();
        }

         protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvBind(); // Bind Gridview on pageload
            }
        }

        // Function which bind gridview control  
        Public void gvBind(){

           connection();
            cmd = new SqlCommand();
            cmd.CommandText = "Select Query"; // best practise is to use storedprocedure
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.CommandTimeout = 0;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
     }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM