Anuj Kumar

Anuj Kumar

  • 1.6k
  • 20
  • 452

Connection error -SQL to ASP.net

Sep 20 2023 1:10 AM

Trying to connect sql databse to aspnet for showing data in gridview with following codes, some issue in this code. Please any one solve this-

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

namespace gridview1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection con = new SqlConnection(@"datasource=DESKTOP-66R7QLC; nitial catalog = SqlPractice; integrated security=sspi");

            //con.Open();
            //SqlCommand cmd = new SqlCommand("select * from Tbl_Employee", con);
            //SqlDataReader rdr = cmd.ExecuteReader();

            ////GridView1.datasource = rdr;
            //GridView1.DataBind();
            //con.Close();
            string connectionString = "Data Source=DESKTOP-66R7QLC;Initial Catalog=SqlPractice";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            string query = "SELECT * FROM Tbl_Employee";
            SqlCommand cmd = new SqlCommand(query, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            GridView1.DataSource = reader;
            GridView1.DataBind();
            connection.Close();

        }
    }
}

 


Answers (6)