Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i have datalist which pick value from my database.so i want when i click image button after that all values are transfer in my griedview.and again click image button second value are transfer my griedview.and one variable store total transfer value.



C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class Main : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["mycon"].ToString();
    Int32 total;
    Int32 quantity;
    //declare an int32 type total which is used in the program  
    DataTable dt = new DataTable();
    //declare a new datatable 

    protected void Page_Load(object sender, EventArgs e)
    {

       
    }
    void getData()
    {


        SqlConnection con = new SqlConnection(connStr);
        SqlDataAdapter da = new SqlDataAdapter("Select * from Items_Details", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlist.DataSource = ds;
        ddlist.DataBind();

    }
    void showdataintotable()
    {
        dt = (DataTable)(Session["ss"]); // this syntax fetches the session and stores it in the datatable//  
        DataRow r1 = dt.NewRow();
        r1[0] = Session["Item_Name"].ToString();       //index of the row and control which puts the value inside the row.  
        r1[1] = Convert.ToInt32(Session["Item_Price"]);
        r1[2] = Convert.ToInt32(Session["Quantity"]);
        dt.Rows.Add(r1);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Int32 a; Int32 i;
        total = 0;
        a = dt.Rows.Count;   // a represents the total no. of rows inside the datatable.  
        for (i = 0; i < a; i++)  // i starts from 0 upto the maximum value of the row.  
        {
            total = total + Convert.ToInt32(dt.Rows[i][3]); // fetches the 4th column of the current row where the value is stored.  
        }

        Label20.Text = total.ToString();
        DataTable ord = new DataTable();
        //ordinary datatable  
        DataColumn dc = new DataColumn();
        //datacolumn which represents the datatable  
        ord.Columns.Add(new DataColumn("Item_Name", Type.GetType("System.String"))); //stores datacolumn name and its datatype  
        ord.Columns.Add(new DataColumn("Sub_Name", Type.GetType("System.String"))); 
        ord.Columns.Add(new DataColumn("Item_Price", Type.GetType("System.Int32")));
        ord.Columns.Add(new DataColumn("Available", Type.GetType("System.Int32")));
        ord.Columns.Add(new DataColumn("Total", Type.GetType("System.Int32")));
        ord.Columns["Total"].Expression = "Item_Price * Available";
        Session["ss"] = ord;
        //put the value of ordinary data table in a session so that it can be used inside table. 
    }
    protected void Button7_Click(object sender, EventArgs e)
    {

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

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

    }
    private void datashow()
    {

        dt = (DataTable)(Session["ss"]); // this syntax fetches the session and stores it in the datatable//  
        DataRow r1 = dt.NewRow();
        r1[0] = Session["name"].ToString();       //index of the row and control which puts the value inside the row.  
        r1[1] = Convert.ToInt32(Session["price"]);
        r1[2] = Convert.ToInt32(Session["quantity"]);
        dt.Rows.Add(r1);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Int32 a; Int32 i;
        total = 0;
        a = dt.Rows.Count;   // a represents the total no. of rows inside the datatable.  
        for (i = 0; i < a; i++)  // i starts from 0 upto the maximum value of the row.  
        {
            total = total + Convert.ToInt32(dt.Rows[i][3]); // fetches the 4th column of the current row where the value is stored.  
        }

        lblsubtotal.Text = total.ToString(); // total amount after execution of the loop.  
    }

    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
        
            //txtquantity.Text = "";
            //panelkeypad.Visible =false;
        ImageButton imgbtn1 = sender as ImageButton;
        DataListItem dl1 = imgbtn1.NamingContainer as DataListItem;

        Label lblid = (Label)dl1.FindControl("Item_Id");
            string id = lblid.Text;

            Label lblnme1 = (Label)dl1.FindControl("Item_Name");
            string nme1 = lblnme1.Text;

            Label lblsub = (Label)dl1.FindControl("Sub_Name");
            string sub = lblsub.Text;

            Label lblprice = (Label)dl1.FindControl("Item_Price");
            string prc = lblprice.Text;

         
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
            con.Open();
            //SqlCommand cmd = new SqlCommand("Select Available from Items_Details where Item_Id='" + id + "' and Item_Name='" + nme1 + "' and Sub_Name='" + sub + "'", con);
            SqlCommand cmd = new SqlCommand("Select * from Items_Details where Item_Id='" + id + "' and Item_Name='" + nme1 + "' and Sub_Name='" + sub + "' and Item_Price='"+ prc +"'", con);
       
            SqlDataReader dr = cmd.ExecuteReader();
            
            if (dr.Read())
            {

                //ImageButton imgbtn = sender as ImageButton;
                //DataListItem dl = imgbtn.NamingContainer as DataListItem;

                //Find Image button in Datalist
                //ImageButton imgbtntxt = (ImageButton)dl.FindControl("img");
                //imglightbox.ImageUrl = imgbtn.ImageUrl;      //Assign imagebutton url to image field in lightbox

                Label ItemId = (Label)ddlist.FindControl("Item_Id");
                string itemid = ItemId.Text;
                iblid.Text = itemid;

                Label lblnme = (Label)ddlist.FindControl("Item_Name");
                string nme = lblnme.Text;
                ItemName.Text = nme;

                Label subname = (Label)ddlist.FindControl("Sub_Name");
                string subnme = subname.Text;
                subname.Text = subnme;

                Label lblprc = (Label)ddlist.FindControl("Item_Price");
                string price = lblprc.Text;
                lblprice2.Text = price;

                ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:ShowImages();", true);

            }
            else
            {
                Response.Write("<script>alert('Sorry Selected Item is not Available')</script>");

            }
        
        //string a;
        //Session["Items_ID"] = a;


        //string a = ddlist.FindControl("Items_IDLabel");
        //showdataintotable();
        ////////Label Items_ID = (Label)ddlist.FindControl("Items_ID");
        ////////Label Item_Name = (Label)ddlist.FindControl("Item_Name");
        ////////Label Sub_Name = (Label)ddlist.FindControl("Sub_Name");
        ////////Label Item_Price = (Label)ddlist.FindControl("Item_Price");
        //////////Label Label8 = (Label)ddlist.FindControl("Label8");
        //////////Label Label8 = (Label)ddlist.FindControl("Label8");
        ////////SqlConnection con = new SqlConnection(connStr);
        ////////con.Open();
        ////////SqlCommand Cmd = new SqlCommand("insert into Sales(Item_Id,Item_Name,Sub_Name,Item_Price) values (@Item_Id,@Item_Name,@Sub_Name,@Item_Price)", con);
        ////////Cmd.Parameters.AddWithValue("@Item_Id", Items_ID.Text);
        ////////Cmd.Parameters.AddWithValue("@Item_Name", Item_Name.Text);
        ////////Cmd.Parameters.AddWithValue("@Sub_Name", Sub_Name.Text);
        ////////Cmd.Parameters.AddWithValue("@Item_Price", Item_Price.Text);
        //////////Cmd.Parameters.AddWithValue("@Quantity", Label8.Text);
        //////////Cmd.Parameters.AddWithValue("@Total", Label8.Text);
        ////////Cmd.CommandType = CommandType.Text;

        ////////int i = Cmd.ExecuteNonQuery();
        ////////if (i > 0)
        ////////{
        ////////    Response.Write("<script>alert('Record Inserted Sucessfully')</script>");
        ////////}
        ////////con.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = (DataTable)(Session["SS"]);
        dt.Rows.RemoveAt(e.RowIndex);
        tempdata();
    }
    private void tempdata()
    //this method envokes the value of datatable and shows the current total even after deletion of row  
    {
        dt = (DataTable)(Session["ss"]);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Cache["data"] = dt;
        Int32 a;
        Int32 i;
        a = dt.Rows.Count;
        for (i = 0; i < a; i++)
        {
            total += Convert.ToInt32(dt.Rows[i][3]);
        }
        lblsubtotal.Text = total.ToString();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
Posted
Updated 17-Oct-12 20:05pm
v2
Comments
AshishChaudha 18-Oct-12 2:48am    
What problem you are facing??

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