Forums - C# Corner

Forum guidelines
archana shenoy

archana shenoy

  • 759
  • 4
  • 3.5k

updating total time value based on time value

Dec 14 2012 1:12 AM

i have a gridview with columns task_no,date,university,spoken,medium ,time and a template field column - total_time.

the column 'time' has a label in the item template and a textbox in the edititemtemplate..

the column 'total_time' has a label in the item template..

what i need is :-

1) value of time should be the value of total_time..

2)as i change the value of time during EDIT,that value should be added up to the value of total_time..

this should be done for each row..

my source code and code behind is :

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;

public partial class DisplayEntry : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        if (!IsPostBack)
        {

            ddlUniv_SelectedIndexChanged(sender, e);
            ddlMedium_SelectedIndexChanged(sender, e);
            ddlUnivv_SelectedIndexChanged(sender, e);
            ddlMediumm_SelectedIndexChanged(sender, e);

            ProCon p = new ProCon();
            try
            {
                DataTable dt = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter();

                p.cmd.CommandText = "select * from entry_mast";


                adp.SelectCommand = p.cmd;
                dt.Clear();

                adp.Fill(dt);
                // GridView1.DataSource = dt;
                GridView1.DataBind();


            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }

        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        long t;
        string dt, sp;
        decimal tt;

        TextBox tb = (TextBox)(GridView1.FooterRow.FindControl("txtTaskNo"));
        t = Convert.ToInt64(tb.Text);
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtDate"));
        dt = tb.Text;
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtSpoken"));
        sp = tb.Text;
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtTime"));
        tt = Convert.ToDecimal(tb.Text);

        DropDownList ddlUniv = (DropDownList)GridView1.FooterRow.Cells[2].FindControl("ddlUniv");
        DropDownList ddlMedium = (DropDownList)GridView1.FooterRow.Cells[4].FindControl("ddlMedium");
        SqlDataSource1.InsertParameters[0].DefaultValue = t.ToString();
        SqlDataSource1.InsertParameters[1].DefaultValue = dt;
        SqlDataSource1.InsertParameters[3].DefaultValue = sp;
        SqlDataSource1.InsertParameters[5].DefaultValue = tt.ToString();
        SqlDataSource1.InsertParameters[2].DefaultValue = ddlUniv.SelectedValue;
        SqlDataSource1.InsertParameters[4].DefaultValue = ddlMedium.SelectedValue;

        SqlDataSource1.Insert();
        Response.Write("SAVED");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void ddlUniv_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlUniv = new DropDownList();
        Response.Write(ddlUniv.SelectedValue);
        ddlUniv.Items.Clear();
    }
    protected void ddlUnivv_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlUniv = new DropDownList();
        Response.Write(ddlUniv.SelectedValue);
    }
    protected void ddlMediumm_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlMedium = new DropDownList();
        Response.Write(ddlMedium.SelectedValue);
    }
    protected void ddlMedium_SelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList ddlMedium = new DropDownList();
        Response.Write(ddlMedium.SelectedValue);
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
     ProCon p=new ProCon();
      p.cnn.Open();
        SqlCommand mydelete = new SqlCommand("delete from entry_mast where task_no=@task_no",p.cnn);
        mydelete.CommandType = CommandType.Text;


        mydelete.Parameters.Add("@task_no", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        mydelete.ExecuteNonQuery();
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
    }

  decimal total_time;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {


 Label lblTime = (Label)e.Row.FindControl("lblTime");
           decimal time = Decimal.Parse(lblTime.Text);

        Label lblTOT = (Label)e.Row.FindControl("lblTOT");

            lblTOT.Text= time.ToString();

            total_time += time;
        }



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

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }


    protected void txtTime_TextChanged(object sender, EventArgs e)
    {


    }
}using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;

public partial class DisplayEntry : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        if (!IsPostBack)
        {

            ddlUniv_SelectedIndexChanged(sender, e);
            ddlMedium_SelectedIndexChanged(sender, e);
            ddlUnivv_SelectedIndexChanged(sender, e);
            ddlMediumm_SelectedIndexChanged(sender, e);

            ProCon p = new ProCon();
            try
            {
                DataTable dt = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter();

                p.cmd.CommandText = "select * from entry_mast";


                adp.SelectCommand = p.cmd;
                dt.Clear();

                adp.Fill(dt);
                // GridView1.DataSource = dt;
                GridView1.DataBind();


            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }

        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        long t;
        string dt, sp;
        decimal tt;

        TextBox tb = (TextBox)(GridView1.FooterRow.FindControl("txtTaskNo"));
        t = Convert.ToInt64(tb.Text);
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtDate"));
        dt = tb.Text;
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtSpoken"));
        sp = tb.Text;
        tb = (TextBox)(GridView1.FooterRow.FindControl("txtTime"));
        tt = Convert.ToDecimal(tb.Text);

        DropDownList ddlUniv = (DropDownList)GridView1.FooterRow.Cells[2].FindControl("ddlUniv");
        DropDownList ddlMedium = (DropDownList)GridView1.FooterRow.Cells[4].FindControl("ddlMedium");
        SqlDataSource1.InsertParameters[0].DefaultValue = t.ToString();
        SqlDataSource1.InsertParameters[1].DefaultValue = dt;
        SqlDataSource1.InsertParameters[3].DefaultValue = sp;
        SqlDataSource1.InsertParameters[5].DefaultValue = tt.ToString();
        SqlDataSource1.InsertParameters[2].DefaultValue = ddlUniv.SelectedValue;
        SqlDataSource1.InsertParameters[4].DefaultValue = ddlMedium.SelectedValue;

        SqlDataSource1.Insert();
        Response.Write("SAVED");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void ddlUniv_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlUniv = new DropDownList();
        Response.Write(ddlUniv.SelectedValue);
        ddlUniv.Items.Clear();
    }
    protected void ddlUnivv_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlUniv = new DropDownList();
        Response.Write(ddlUniv.SelectedValue);
    }
    protected void ddlMediumm_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlMedium = new DropDownList();
        Response.Write(ddlMedium.SelectedValue);
    }
    protected void ddlMedium_SelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList ddlMedium = new DropDownList();
        Response.Write(ddlMedium.SelectedValue);
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
     ProCon p=new ProCon();
      p.cnn.Open();
        SqlCommand mydelete = new SqlCommand("delete from entry_mast where task_no=@task_no",p.cnn);
        mydelete.CommandType = CommandType.Text;


        mydelete.Parameters.Add("@task_no", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        mydelete.ExecuteNonQuery();
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
    }

  decimal total_time;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {


 Label lblTime = (Label)e.Row.FindControl("lblTime");
           decimal time = Decimal.Parse(lblTime.Text);

        Label lblTOT = (Label)e.Row.FindControl("lblTOT");

            lblTOT.Text= time.ToString();

            total_time += time;
        }



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

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }


    protected void txtTime_TextChanged(object sender, EventArgs e)
    {


    }
}



source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayEntry.aspx.cs" Inherits="DisplayEntry" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

<%--
--%>







<%--


--%>



        DataKeyNames="task_no" DataSourceID="SqlDataSource1"
        EnableModelValidation="True" ShowFooter="True"

        style="z-index: 1; left: 104px; top: 148px; position: absolute; height: 133px; width: 187px"
        onpageindexchanging="GridView1_PageIndexChanging"
        onrowdatabound="GridView1_RowDataBound" onrowdeleted="GridView1_RowDeleted"
        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand"
        onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating"
        onselectedindexchanged="GridView1_SelectedIndexChanged" AllowPaging="True"
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
        CellPadding="3" GridLines="Horizontal" PageSize="3"
        >





















































                         SelectedValue='<%# Bind("university") %>'
                         onselectedindexchanged="ddlUnivv_SelectedIndexChanged">
delhi
chennai
bombay
manipal





                         AppendDataBoundItems="True" EnableViewState="False"
                         OnSelectedIndexChanged="ddlUniv_SelectedIndexChanged"
                      >
--select--
delhi
chennai
bombay
manipal

                      






































                         AppendDataBoundItems="True"
                            onselectedindexchanged="ddlMediumm_SelectedIndexChanged" >
Web
Mail



                        EnableViewState="False"
                            onselectedindexchanged="ddlMedium_SelectedIndexChanged">
--select--
Web
Mail

                         





















                        Width="64px" />





















        ConnectionString="<%$ ConnectionStrings:con %>"

        SelectCommand="SELECT * FROM [entry_mast]"
        ConflictDetection="CompareAllValues"
        DeleteCommand="DELETE FROM [entry_mast] WHERE [task_no] = @original_task_no AND (([date] = @original_date) OR ([date] IS NULL AND @original_date IS NULL)) AND (([university] = @original_university) OR ([university] IS NULL AND @original_university IS NULL)) AND (([spoken] = @original_spoken) OR ([spoken] IS NULL AND @original_spoken IS NULL)) AND (([medium] = @original_medium) OR ([medium] IS NULL AND @original_medium IS NULL)) AND (([time] = @original_time) OR ([time] IS NULL AND @original_time IS NULL))"
        InsertCommand="INSERT INTO [entry_mast] ([task_no], [date], [university], [spoken], [medium], [time]) VALUES (@task_no, @date, @university, @spoken, @medium, @time)"
        OldValuesParameterFormatString="original_{0}"




        UpdateCommand="UPDATE [entry_mast] SET [date] = @date, [university] = @university, [spoken] = @spoken, [medium] = @medium, [time] = @time WHERE [task_no] = @original_task_no AND (([date] = @original_date) OR ([date] IS NULL AND @original_date IS NULL)) AND (([university] = @original_university) OR ([university] IS NULL AND @original_university IS NULL)) AND (([spoken] = @original_spoken) OR ([spoken] IS NULL AND @original_spoken IS NULL)) AND (([medium] = @original_medium) OR ([medium] IS NULL AND @original_medium IS NULL)) AND (([time] = @original_time) OR ([time] IS NULL AND @original_time IS NULL))">


































<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayEntry.aspx.cs" Inherits="DisplayEntry" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

<%--
--%>







<%--


--%>



        DataKeyNames="task_no" DataSourceID="SqlDataSource1"
        EnableModelValidation="True" ShowFooter="True"

        style="z-index: 1; left: 104px; top: 148px; position: absolute; height: 133px; width: 187px"
        onpageindexchanging="GridView1_PageIndexChanging"
        onrowdatabound="GridView1_RowDataBound" onrowdeleted="GridView1_RowDeleted"
        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand"
        onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating"
        onselectedindexchanged="GridView1_SelectedIndexChanged" AllowPaging="True"
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
        CellPadding="3" GridLines="Horizontal" PageSize="3"
        >





















































                         SelectedValue='<%# Bind("university") %>'
                         onselectedindexchanged="ddlUnivv_SelectedIndexChanged">
delhi
chennai
bombay
manipal





                         AppendDataBoundItems="True" EnableViewState="False"
                         OnSelectedIndexChanged="ddlUniv_SelectedIndexChanged"
                      >
--select--
delhi
chennai
bombay
manipal

                      






































                         AppendDataBoundItems="True"
                            onselectedindexchanged="ddlMediumm_SelectedIndexChanged" >
Web
Mail



                        EnableViewState="False"
                            onselectedindexchanged="ddlMedium_SelectedIndexChanged">
--select--
Web
Mail

                         





















                        Width="64px" />





















        ConnectionString="<%$ ConnectionStrings:con %>"

        SelectCommand="SELECT * FROM [entry_mast]"
        ConflictDetection="CompareAllValues"
        DeleteCommand="DELETE FROM [entry_mast] WHERE [task_no] = @original_task_no AND (([date] = @original_date) OR ([date] IS NULL AND @original_date IS NULL)) AND (([university] = @original_university) OR ([university] IS NULL AND @original_university IS NULL)) AND (([spoken] = @original_spoken) OR ([spoken] IS NULL AND @original_spoken IS NULL)) AND (([medium] = @original_medium) OR ([medium] IS NULL AND @original_medium IS NULL)) AND (([time] = @original_time) OR ([time] IS NULL AND @original_time IS NULL))"
        InsertCommand="INSERT INTO [entry_mast] ([task_no], [date], [university], [spoken], [medium], [time]) VALUES (@task_no, @date, @university, @spoken, @medium, @time)"
        OldValuesParameterFormatString="original_{0}"




        UpdateCommand="UPDATE [entry_mast] SET [date] = @date, [university] = @university, [spoken] = @spoken, [medium] = @medium, [time] = @time WHERE [task_no] = @original_task_no AND (([date] = @original_date) OR ([date] IS NULL AND @original_date IS NULL)) AND (([university] = @original_university) OR ([university] IS NULL AND @original_university IS NULL)) AND (([spoken] = @original_spoken) OR ([spoken] IS NULL AND @original_spoken IS NULL)) AND (([medium] = @original_medium) OR ([medium] IS NULL AND @original_medium IS NULL)) AND (([time] = @original_time) OR ([time] IS NULL AND @original_time IS NULL))">




































i am getting same value for both time and total_time..


and when i try to edit, i am gettin error:Object reference not set to instance of an object..

however delete is workin fine :)

and when i try to edit, i am gettin error:Object reference not set to instance of an object..

however delete is workin fine :)



plzz help..its very very urgent!!


Answers (1)

Cloud & IoT DevCon16

SPONSORED BY

Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.