Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Daily.cs

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Bill
{
    public partial class Daily : Form
    {
        Store objStore = new Store(); 
        public Daily()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string date1 = dateTimePicker1.Text;            
            DateTime dat = DateTime.Parse(date1.ToString());
            string dat1 = dat.ToShortDateString();
            objStore.date = dat1.ToString();

            int S, X, Y;
            if (objStore.GetData())
            {
                S = objStore.price;
                lblSalesIncome.Text = S.ToString();
            }          
            else 
            {
               S=0;
            }

            if (objStore.GetSum())
            {
                X = objStore.price;
                lblPurchaseExpense.Text = X.ToString();
            }
            else
            {
                X = 0;
            }
            if (objStore.GetExpense())
            {
                Y = objStore.total;
                lblExpense.Text = Y.ToString();
            }
            else
            {
                Y = 0;
            }
            int totalIncomne = S;
            int expense = X + Y;
            lblExpTotal.Text = expense.ToString();
            lblTotalIncome.Text = S.ToString();
            int ss = totalIncomne - expense;
            lblbal.Text = ss.ToString();
        }

        private void Daily_Load(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Text;
            int S, X, Y;
            DateTime dat =DateTime.Parse(date.ToString());
            string dat1 = dat.ToString("MM/dd/yyyy");
            objStore.date = dat1.ToString();
            if (objStore.GetData())
            {
                S = objStore.price;
                lblSalesIncome.Text = S.ToString();
            }

            else
            {
                S = 0;
            }

            if (objStore.GetSum())
            {
                X = objStore.price;
                lblPurchaseExpense.Text = X.ToString();
            }
            else
            {
                X = 0;
            }
            if (objStore.GetExpense())
            {
                Y = objStore.total;
                lblExpense.Text = Y.ToString();
            }
            else
            {
                Y = 0;
            }
            int totalIncomne = S;
            int expense = X + Y;
            lblExpTotal.Text = expense.ToString();
            lblTotalIncome.Text = S.ToString();
            int ss = totalIncomne - expense;
            lblbal.Text = ss.ToString();
        }
    }
}

store.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Billing.Dal;
using System.Data.SqlClient;
using System.Data;
namespace Bill
{
    class Store
    {
public string date { get; set; }

internal bool GetData()
        {
            DalLayer objdal = new DalLayer();
            SqlCommand cmd = new SqlCommand("select sum(Total) from tblSalesEntry where Date=@Date");
            cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
            DataTable dt = objdal.CudReturnDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                price = Convert.ToInt32(dt.Rows[0][0].ToString());
                return true;
            }
            else
            {
                return false;
            }
        }

        internal bool GetSum()
        {
            DalLayer objdal = new DalLayer();
            SqlCommand cmd = new SqlCommand("select sum(Price) from tblPurchase where Date=@Date");
            cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
            DataTable dt = objdal.CudReturnDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                price = Convert.ToInt32(dt.Rows[0][0].ToString());
                return true;
            }
            else
            {
                return false;
            }
        }

        internal bool GetExpense()
        {
            DalLayer objdal = new DalLayer();
            SqlCommand cmd = new SqlCommand("select sum(fldAdvance) from tblExp where Date=@Date");
            cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
            DataTable dt = objdal.CudReturnDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                total = Convert.ToInt32(dt.Rows[0][0].ToString());
                return true;
            }
            else
            {
                return false;
            }
        }
}
}


Dal.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Dal
/// </summary>
/// 
namespace Billing.Dal
{
    public class DalLayer
    {
        #region Variables
        public string conStr = ConfigurationManager.ConnectionStrings["Bill.Properties.Settings.BillConnectionString"].ConnectionString;

        public DalLayer()
        { }
        #endregion
public DataTable CudReturnDataTable(SqlCommand cmd)
        {
            SqlConnection con = new SqlConnection();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            con.ConnectionString = conStr;
            try
            {
                using (con)
                {
                     cmd.Connection = con;
                    con.Open();
                    SqlTransaction trans = con.BeginTransaction();
                    using (cmd)
                    {
                        using (trans)
                        {
                            try
                            {
                                cmd.Transaction = trans;
                                da.Fill(dt);
                                da.Dispose();
                                cmd.Parameters.Clear();
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.Rollback();
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
            }

            return dt;
        }
Posted
Updated 14-Oct-12 20:59pm
v2
Comments
vasim sajad 15-Oct-12 2:18am    
in which line it throwing exception ? debug it and tell wer it it throwing exception ?
ravuravu 15-Oct-12 2:29am    
actually the error showing that input string was not in a correct format
make sure your method arguments are in correct format
when converting a string to datetime,parse the string to take the date before putting each variable into the datetime object
get general help of this exception
ravuravu 15-Oct-12 2:31am    
Error showing in the line that was
internal GetData()
{
DalLayer objdal = new DalLayer();
SqlCommand cmd = new SqlCommand("select sum(Total) from tblSalesEntry where Date=@Date");
cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
DataTable dt = objdal.CudReturnDataTable(cmd);
if (dt.Rows.Count > 0)
{
price = Convert.ToInt32(dt.Rows[0][0].ToString());
return true;
}
else
{
return false;
}
}

in this price=convert.int32 that line shows the error
ravuravu 15-Oct-12 2:33am    
when am selecting a date that occurs the value i got the answer but when am selecting a date that do not occur any values i got this error
ravuravu 15-Oct-12 2:34am    
i think actually exception is not working,any one pls help me

Just one line I spotted tells me that you don't understand what are you doing, in principle:
C#
DateTime dat = DateTime.Parse(date.ToString());


Even before seeing the declaration of data (your naming style is a separate song), this becomes clear. If it was DateType, there is no sense to converted it to string and parse back, but if it's string, there is no sense in getting ToString from something which is already a string. Looking for the declaration, we see that it's declared as string. OK, you are not ready to write what you are writing.

And the exception is because the string cannot be interpreted as DateTime. If, for example, the input string is "1-2 blah-blah", the parse would fail, isn't that natural? You could find out what really happens if you executed it under the debugger.

By the way, it's not nice to ask about exception not telling us in what line it was thrown.

—SA
 
Share this answer
 
v2
Comments
ravuravu 15-Oct-12 2:49am    
can u pls correct it
Sergey Alexandrovich Kryukov 15-Oct-12 11:02am    
I don't think it makes sense. You need to debug your code to see what is the value of some string and why, and then re-write the code the way you understand each line exactly, in an accurate manner. If you have doubts about particular places, I'll gladly help.
--SA
ravuravu 15-Oct-12 13:23pm    
sir i Can't Debug,when i try to debug that time it shows error.if i try to run, it works and i can get the values if purchase,salesentry,expense occure.
Sergey Alexandrovich Kryukov 15-Oct-12 14:04pm    
"Expense occure"?! Sometimes just correct spelling helps to find a problem. :-)
You always can debug. It's a matter where your break point is. If you set a break point on the beginning of each method in question, it will be enough. Besides, if you simply run it under debugger, it will show you where the exception is.
--SA
ravuravu 15-Oct-12 13:30pm    
if any one of them do not occure then it not works
Check this for different DateTime formats - Easy String to DateTime, DateTime to String and Formatting[^] Hope it will help you to understand the DateTime conversion.
 
Share this answer
 
Comments
ravuravu 15-Oct-12 3:13am    
i cant correct it pls help me
ravuravu 15-Oct-12 3:24am    
pls help me
ravuravu 15-Oct-12 5:52am    
hey pls help me
ravuravu 15-Oct-12 8:04am    
I Cant, Pls help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900