输入字符串格式不正确(我认为是异常错误) [英] Input String Was not in acorrect format(i think exception errorr)

查看:56
本文介绍了输入字符串格式不正确(我认为是异常错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Daily.cs

Daily.cs

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


store.cs

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



Dal.cs

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;
        }

推荐答案

我发现的一行仅告诉我,您原则上不了解您在做什么:
Just one line I spotted tells me that you don''t understand what are you doing, in principle:
DateTime dat = DateTime.Parse(date.ToString());



甚至在看到data的声明(您的命名方式是一首单独的歌曲)之前,这一点就变得很清楚.如果它是DateType,则没有意义将其转换为字符串并进行解析,但是,如果它是字符串,则从已经是字符串的东西中获取ToString是没有意义的.在查找声明时,我们看到它声明为字符串.好的,您还没准备好写您正在写的东西.

例外是因为字符串不能解释为DateTime.例如,如果输入字符串为"1-2 blah-blah",则解析将失败,这不是很自然吗?如果在调试器中执行它,您将发现会发生什么.

顺便问一句,问异常而不告诉我们抛出异常是不好的事情.

-SA



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


检查此日期是否具有不同的DateTime格式-
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.


这篇关于输入字符串格式不正确(我认为是异常错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆