-1

public partial class AddToCart : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add("sno"); dt.Columns.Add("ProductID"); dt.Columns.Add("ProductName"); dt.Columns.Add("Price"); dt.Columns.Add("ProductImage"); dt.Columns.Add("Cost"); dt.Columns.Add("TotalCost");

        if (Request.QueryString["id"] != null)
        {
            if (Session["Buyitems"] == null)
            {

                dr = dt.NewRow();
                String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
                SqlConnection scon = new SqlConnection(mycon);
                String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = myquery;
                cmd.Connection = scon;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                dr["sno"] = 1;
                dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
                dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
                dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
                dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Session["buyitems"] = dt;
            }
            else
            {

                dt = (DataTable)Session["buyitems"];
                int sr;
                sr = dt.Rows.Count;

                dr = dt.NewRow();
                String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
                SqlConnection scon = new SqlConnection(mycon);
                String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = myquery;
                cmd.Connection = scon;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                dr["sno"] = sr + 1;
                dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
                dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
                dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
                dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Session["buyitems"] = dt;

            }
        }
        else
        {
            dt = (DataTable)Session["buyitems"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
Meti Rs
  • 3
  • 2
  • Welcom @Meti Rs. Any more information? Just codes doesn't enough. You should describe what problem you met and what you want. – Shawn Xiao May 26 '20 at 04:15
  • add error details – Always_a_learner May 26 '20 at 04:39
  • when i click on the add to card button it's show me this and page still trying to load da.Fill(ds); sqlExeption was unhandled by user code A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) – Meti Rs May 26 '20 at 05:03

1 Answers1

0

Update your connection string like below. You are missing to specify whether you want to use Windows Authentication or User Id & Password.

For Windows Authentication use Integrated Security=SSPI as below :

String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=SSPI";

For Authentication with User Id & Password add User Id & Password as below. Use original user id and password. I have taken sa for example. :

String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True;user id=sa;password=sa";

Also you need to open connection with

SqlConnection scon = new SqlConnection(mycon);
scon.Open(); //Open connection

P.S. It is always recommended to Close connection too. Add scon.Open(); after da.Fill(ds); line. Why always close Database connection? .

da.Fill(ds);
scon.Close(); //Close connection
Karan
  • 12,059
  • 3
  • 24
  • 40