Skip to content
Loading
  • +3
    This code is in  Cart.aspx Page:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using paytm;
    namespace WebApplication2
    {
    public partial class WebForm13 : System.Web.UI.Page
    {
    int cart_sub_total = 0;
    int shiping_cost = 0;
    SqlConnection con;
    SqlConnection con1;
    SqlDataAdapter da;
    SqlCommand cmd;
    string cons = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
    lbl_shipping_cost.Text = "70";
    if (!IsPostBack)
    {
    if (Session["Buyitem"] == null)
    {
    Buy.Enabled = false;
    }
    }
    else
    {
    Buy.Enabled = true;
    }
    Session["addproduct"] = true;
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add("sno");
    dt.Columns.Add("Product_Name");
    dt.Columns.Add("ProdImage");
    dt.Columns.Add("ProdPrice");

    if (Request.QueryString["ProductID"] != null)
    {
    if (Session["Buyitem"] == null)
    {
    dr = dt.NewRow();
    con = new SqlConnection(cons);
    da = new SqlDataAdapter("select * from Product where ProductID=" + Request.QueryString["ProductID"], con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dr["sno"] = 1;
    dr["Product_Name"] = ds.Tables[0].Rows[0]["Product_Name"].ToString();
    dr["ProdImage"] = ds.Tables[0].Rows[0]["ProdImage"].ToString();
    dr["ProdPrice"] = ds.Tables[0].Rows[0]["ProdPrice"].ToString();
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Session["Buyitem"] = dt;
    Buy.Enabled = true;
    GridView1.FooterRow.Cells[3].Text = "Total Amount Rs.";
    GridView1.FooterRow.Cells[4].Text = GrandTotal().ToString();
    lbl_cart_sub_total.Text = GrandTotal().ToString();
    cart_sub_total = Convert.ToInt32(lbl_cart_sub_total.Text);
    shiping_cost = Convert.ToInt32(lbl_shipping_cost.Text);
    lbl_total.Text = (cart_sub_total + shiping_cost).ToString();
    Response.Redirect("Cartt.aspx");
    Session["grandTotal"] = lbl_total.Text;

    }
    else
    {
    dt = (DataTable)Session["Buyitem"];
    int sr;
    sr = dt.Rows.Count;
    dr = dt.NewRow();
    con1 = new SqlConnection(cons);
    da = new SqlDataAdapter("select * from Product where ProductID=" + Request.QueryString["ProductID"], con1);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dr["sno"] = sr + 1;
    dr["Product_Name"] = ds.Tables[0].Rows[0]["Product_Name"].ToString();
    dr["ProdImage"] = ds.Tables[0].Rows[0]["ProdImage"].ToString();
    dr["ProdPrice"] = ds.Tables[0].Rows[0]["ProdPrice"].ToString();
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Session["Buyitem"] = dt;
    Buy.Enabled = true;
    GridView1.FooterRow.Cells[3].Text = "Total Amount Rs.";
    GridView1.FooterRow.Cells[4].Text = GrandTotal().ToString();
    lbl_cart_sub_total.Text = GrandTotal().ToString();
    cart_sub_total = Convert.ToInt32(lbl_cart_sub_total.Text);
    shiping_cost = Convert.ToInt32(lbl_shipping_cost.Text);
    lbl_total.Text = (cart_sub_total + shiping_cost).ToString();
    Response.Redirect("Cartt.aspx");
    Session["grandTotal"] = lbl_total.Text;

    }
    }
    else
    {
    dt = (DataTable)Session["Buyitem"];
    GridView1.DataSource = dt;
    GridView1.DataBind();
    if (GridView1.Rows.Count > 0)
    {
    GridView1.FooterRow.Cells[3].Text = "Total Amount Rs.";
    GridView1.FooterRow.Cells[4].Text = GrandTotal().ToString();
    lbl_cart_sub_total.Text = GrandTotal().ToString();
    cart_sub_total = Convert.ToInt32(lbl_cart_sub_total.Text);
    shiping_cost = Convert.ToInt32(lbl_shipping_cost.Text);
    lbl_total.Text = (cart_sub_total + shiping_cost).ToString();
    lbl_countcart.Text = GridView1.Rows.Count.ToString();
    Session["Cart"] = GridView1.Rows.Count.ToString();
    Session["grandTotal"]= lbl_total.Text;
    }
    }
    string orderdate = DateTime.Now.ToShortDateString();
    Session["Orderdate"] = orderdate;
    orderid();
    }
    public int GrandTotal()
    {
    DataTable dt = new DataTable();
    dt = (DataTable)Session["Buyitem"];
    int nrow = dt.Rows.Count;
    int i = 0;
    int TotalPrice = 0;
    while (i < nrow)
    {
    TotalPrice = TotalPrice + Convert.ToInt32(dt.Rows[i]["ProdPrice"].ToString());
    i = i + 1;
    }
    return TotalPrice;
    }
    public void orderid()
    {
    string alpha = "1234567890";
    Random ran = new Random();
    char[] myarray = new char[5];
    for (int i = 0; i < 5; i++)
    {
    myarray[i] = alpha[(int)(10 * ran.NextDouble())];
    }
    string orderid;
    orderid = DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + new string(myarray) + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
    Session["OrderID"] = orderid;
    }
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
    {
    }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    DataTable dt = new DataTable();
    dt = (DataTable)Session["Buyitem"];
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
    int sr;
    int sr1;
    string Qdata;
    string Dtdata;
    sr = Convert.ToInt32(dt.Rows[i]["sno"].ToString());
    TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];
    Qdata = cell.Text;
    Dtdata = sr.ToString();
    sr1 = Convert.ToInt32(Qdata);
    if (sr == sr1)
    {
    dt.Rows[i].Delete();
    dt.AcceptChanges();
    break;
    }
    }
    for (int i = 1; i <= dt.Rows.Count; i++)
    {
    dt.Rows[i - 1]["sno"] = i;
    dt.AcceptChanges();
    }
    Session["Buyitem"] = dt;
    Response.Redirect("Cartt.aspx");
    }
    protected void Buy_Click1(object sender, EventArgs e)
    {
    if (txt_Name.Text == "")
    {
    string script = "alert(\"Please Enter Name !\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
    }
    else if (txt_BillingAddress.Text == "")
    {
    string script = "alert(\"Please Enter Shipping Address !\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
    }
    else if (txt_PIN.Text == "")
    {
    string script = "alert(\"Please Enter PIN !\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
    }
    else if (txt_mob.Text == "")
    {
    string script = "alert(\"Please Enter Mobile No. !\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
    }
    else
    {
    //this.payment();
    if (Session["UserID"] != null)
    {
    string USERID = Session["UserID"].ToString();
    string PaymentType = "Paytm";
    string PaymentStatus = "NotPaid";
    DateTime DateofPurchase = DateTime.Now;
    string EMAILID = Session["UserID"].ToString();
    string Cust_ID= Session["UserID"].ToString();
    string OrderID = Session["OrderID"].ToString();
    string Txn_Amount = Session["grandTotal"].ToString();
    string CallbackURL = "https://localhost:44348/PaytmPaymentGateway.aspx";
    PaytmPayment(EMAILID, Cust_ID, OrderID, Txn_Amount, CallbackURL);
    Session["CstmrName"] = txt_Name.Text;
    Session["BillingAddress"] = txt_BillingAddress.Text;
    Session["Pin"] = txt_PIN.Text;
    Session["Mob"] = txt_mob.Text;
    //this.getOrderDetail();
    }
    }
    }
    public void PaytmPayment(string EMAILID,string Cust_ID,string OrderID,string Txn_Amount,string CallbackURL)
    {
    string userr = Request.Cookies["BUEMAIL"].Value;
    String merchantKey = "AAAAAAAAAAAAAAAAA";
    Dictionary parameters = new Dictionary();
    parameters.Add("MID", "XXXXXXXXXXXXXXX");
    parameters.Add("CHANNEL_ID", "WEB");
    parameters.Add("INDUSTRY_TYPE_ID", "Retail");
    parameters.Add("WEBSITE", "WEBSTAGING");
    parameters.Add("EMAIL", EMAILID);
    parameters.Add("MOBILE_NO", "0123456789");
    parameters.Add("CUST_ID", Cust_ID);
    parameters.Add("ORDER_ID", OrderID);
    parameters.Add("TXN_AMOUNT", Txn_Amount);
    parameters.Add("CALLBACK_URL", CallbackURL); //This parameter is not mandatory. Use this to pass the callback url dynamically.
    string checksum = CheckSum.generateCheckSum(merchantKey, parameters);
    string paytmURL = "https://securegw-stage.paytm.in/theia/processTransaction";
    string outputHTML = "";
    outputHTML = NewMethod(outputHTML);
    outputHTML += "Merchant Check Out Page";
    outputHTML += "";
    outputHTML += "";
    outputHTML += "

    Please do not refresh this page...

    ";
    outputHTML += "
    ";
    outputHTML += "";
    outputHTML += "";
    foreach (string key in parameters.Keys)
    {
    outputHTML += "";
    }
    outputHTML += "";
    outputHTML += "";
    outputHTML += "
    ";
    outputHTML += "";
    outputHTML += "
    ";
    outputHTML += "";
    outputHTML += "";
    Response.Write(outputHTML);

    }
    private static string NewMethod(string outputHTML)
    {
    outputHTML += "";
    return outputHTML;
    }

    }
    }
     
     
    and below code is for PaytmPaymentGateway.aspx Page:
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using paytm;
    namespace WebApplication2
    {
    public partial class PaytmPaymentGateway : System.Web.UI.Page
    {
    SqlConnection con;
    SqlCommand cmd;
    string cons = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
    String merchantKey = "XXXXXXXXXXXXXX";
    Dictionary parameters = new Dictionary();
    string paytmChecksum = "";
    foreach (string key in Request.Form.Keys)
    {
    parameters.Add(key.Trim(), Request.Form[key].Trim());
    }
    if (parameters.ContainsKey("CHECKSUMHASH"))
    {
    paytmChecksum = parameters["CHECKSUMHASH"];
    parameters.Remove("CHECKSUMHASH");
    }
    if (CheckSum.verifyCheckSum(merchantKey, parameters, paytmChecksum))
    {
    string paytmStatus = parameters["STATUS"];
    string txnID = parameters["TXNID"];
    if (paytmStatus == "TXN_SUCCESS")
    {
    hiMessage.InnerText = "Payment Success";
    pTxnID.InnerText = "Transaction ID :" + txnID;
    this.getOrderDetail();
    }
    else if (paytmStatus == "PENDING")
    {
    hiMessage.InnerText = "Payment is Pending";
    }
    else if (paytmStatus == "TXN_FAILURE")
    {
    hiMessage.InnerText = "Payment Failure !";
    }
    }
    else
    {
    Response.Write("Checksum MisMatch");
    }
    }
    protected void getOrderDetail()
    {
    DataTable dt;
    dt = (DataTable)Session["Buyitem"];
    if (dt != null)
    {
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
    con = new SqlConnection(cons);
    con.Open();
    string sql = ("Insert into OrderDetails(OrderID,ProductName,ProductImage,Price,CustomerName,BillingAddress,OrderDate,OrderStatus)values('" + Session["OrderID"] + "','" + dt.Rows[i]["Product_Name"] + "','" + dt.Rows[i]["ProdImage"] + "','" + dt.Rows[i]["ProdPrice"] + "','" + Session["CstmrName"] + "','" + Session["BillingAddress"] + "','" + Session["Orderdate"] + "','Pending')");
    cmd = new SqlCommand(sql, con);
    cmd.ExecuteNonQuery();
    con.Close();
    Session["Buyitem"] = null;
    }
    }
    else
    {
    }
    }
    /* protected void Button1_Click(object sender, EventArgs e)
    {
    this.getOrderDetail();
    Response.Redirect("BooksWorld_Index.aspx");
    }*/
    }
    }
     
    Plz review the code and helpme 
    +3
  • Jenith Thakkar
    +3
    Hi 
    Might be intialization issue

    1. DataTable dt;    
    2. dt = (DataTable)Session["Buyitem"];    
    in place of this try

    1. DataTable dt = new DataTable();    
    2. dt = (DataTable)Session["Buyitem"];    
    This
    +3
  • Atul Warade
    +3
    Hi Naveen,
     
    Try to use this one
    1. DataTable dt = new DataTable();  
    2. dt = (DataTable)Session["Buyitem"];  
    Also Dont set
     
    Session["Buyitem"] = null;
     
    instead of use
     
    Session["Buyitem"] = dt;
    +3
  • +2
    As @jenith mentioned earlier, check for dt!=null case. if dt have the data it will go through the code and if it doesn't then it won't throw the error.
     
    If dt won't be null ever check your code and debug to see where the problem is. use the below code.
    1. DataTable dt;  
    2. dt = (DataTable)Session["Buyitem"];  
    3. if(dt!=null)  
    4. {  
    5. for (int i = 0; i <= dt.Rows.Count - 1; i++)  
    6. {  
    7. con = new SqlConnection(cons);  
    8. con.Open();  
    9. string sql = ("Insert into OrderDetails(OrderID,ProductName,ProductImage,Price,CustomerName,BillingAddress,OrderDate,OrderStatus)values('" + Session["OrderID"] + "','" + dt.Rows[i]["Product_Name"] + "','" + dt.Rows[i]["ProdImage"] + "','" + dt.Rows[i]["ProdPrice"] + "','" + Session["CstmrName"] + "','" + Session["BillingAddress"] + "','" + Session["Orderdate"] + "','Pending')");  
    10. cmd = new SqlCommand(sql, con);  
    11. cmd.ExecuteNonQuery();  
    12. con.Close();  
    13. Session["Buyitem"] = null;  
    14. }  
    15. }  
    +2
  • Jenith Thakkar
    +2
    Hi

    1. da = new SqlDataAdapter("select * from Product where ProductID=" + Request.QueryString["ProductID"], con);  
    2. DataSet ds = new DataSet();  
    3. da.Fill(ds);  
    4. dr["sno"] = 1;  
    5. dr["Product_Name"] = ds.Tables[0].Rows[0]["Product_Name"].ToString();  
    6. dr["ProdImage"] = ds.Tables[0].Rows[0]["ProdImage"].ToString();  
    7. dr["ProdPrice"] = ds.Tables[0].Rows[0]["ProdPrice"].ToString();  
    8. dt.Rows.Add(dr);  
    9. GridView1.DataSource = dt;  
    10. GridView1.DataBind();  
    11. Session["Buyitem"] = dt;  
    While you debug this code, Have you got the value of dt where I had marked yellow?
    +2
  • Jenith Thakkar
    +1
    Hi, Before start the for loop check dt is not null
    if(dt!=null)
    {
    for(..)
    {
    } 
    } 
    +1
  • +1
    @jenith thanks for post. But in this page i alwayas getting dt null whille i want to get the dt value
    +1
  • Jenith Thakkar
    +1
    Store datatable in session Like
    1. DataTable Tissues = new DataTable();  
    2.   
    3. Tissues = dal.returnTissues("TestID", "TestValue");// returnTissues("","") sample     function for adding values  
    4.   
    5.   
    6. Session.Add("Tissues", Tissues);  
    And retrive it
    1. DataTable Tissues = Session["Tisstues"] as DataTable
    Try with this above syntax
    Update me it's working or not
    +1
  • +1
    @jenith its not working. I also posted whole code . Plz review it. thanks
    +1
  • +1
    @Jenith
     
    Yes, I got the value of dt where You had marked yellow .
    +1
  • +1
    Plz help any one
    +1
  • +1
    @jenith I have tried this but issue persist same
    +1
  • +1
    @Atul I tried this but problem still persist same
    +1
  • +1
    Can I send DataTable Value by QueryString with Response.redirect from one page to another?If yes then how to retrieve value on 2nd page without typcasting problem?
    +1
  • +1
    Thanx @Jenith
    +1
  • Jignesh Kumar
    +1
    Hi,
     
    Please use below code,
    1. DataTable dt = new DataTable();  
    2. if(Session["Buyitem"] != null)  
    3. {  
    4. dt = (DataTable)Session["Buyitem"];    
    5. if(dt != null)  
    6. {  
    7. for (int i = 0; i <= dt.Rows.Count - 1; i++)    
    8. {    
    9. con = new SqlConnection(cons);    
    10. con.Open();    
    11. string sql = ("Insert into OrderDetails(OrderID,ProductName,ProductImage,Price,CustomerName,BillingAddress,OrderDate,OrderStatus)values('" + Session["OrderID"] + "','" + dt.Rows[i]["Product_Name"] + "','" + dt.Rows[i]["ProdImage"] + "','" + dt.Rows[i]["ProdPrice"] + "','" + Session["CstmrName"] + "','" + Session["BillingAddress"] + "','" + Session["Orderdate"] + "','Pending')");    
    12. cmd = new SqlCommand(sql, con);    
    13. cmd.ExecuteNonQuery();    
    14. con.Close();    
    15. Session["Buyitem"] = null;    
    16. }    
    17. }  
    18. }  
     
    +1
  • Hamid Khan
    +1
    I Think you did all the good jobs,
    You only need to create the instance here like this
    DataTable dt = new DataTable();  
    +1