无法转换日期时间 [英] unable to convert datetime

查看:83
本文介绍了无法转换日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是这个行业的新手.我的代码在这里有问题.我收到一条错误消息字符串识别为无法转换日期时间",另一个错误是)"附近的语法不正确.您能否检查以下代码并帮助我解决此问题.
的确,我会很感激您.



请尽快回复我.请有点紧急.

代码在这里:

Hi All,
I am new into this industry. I have problem with my code here. I am getting an error message "string recognised unable to convert datetime" and one more error is incorrect syntax near '')''. Could you please check the following code and help me resolve the issue.
I would be thankful to you indeed.



Kindly reply me at earliest. Its a bit urgent please.

HERE IS THE CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.Data.OleDb;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;


namespace MasterPage
{
    public partial class ReportForWorkOrderD : Form
    {
        public ReportForWorkOrderD()
        {
            InitializeComponent();
        }
     
        SqlConnection con;
        string t = "";
        private void ReportForWorkOrderD_Load(object sender, EventArgs e)
        {
            t = ConfigurationManager.AppSettings.Get("sqlcon");
            con = new SqlConnection(t);
            SqlCommand cmd = new SqlCommand("select distinct(districts) from locationmaster", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            cmbDistricts.Items.Clear();
            while (dr.Read())   
            {
                cmbDistricts.Items.Add(dr[0].ToString());
                //lstDistricts.Items.Add(dr[0].ToString());
            }
            dr.Close();
            con.Close();
            SqlCommand cmd1 = new SqlCommand("select distinct(engineername) from engineermaster", con);
            con.Open();
            SqlDataReader dr1 = cmd1.ExecuteReader();
          
            cmbAllotedTo.Items.Clear();
            while (dr1.Read())
            {
                
                cmbAllotedTo.Items.Add(dr1[0].ToString());
            }
            dr1.Close();
            con.Close();
        }
        CrystalReportForWorkOrderD crwod;
        private void cmbDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("(select distinct(system) from salesorder where location in (select locationname from locationmaster where districts =''" + cmbDistricts.Text + "''))union(select distinct(system) from amcorder where location in (select locationname from locationmaster where districts =''" + cmbDistricts.Text + "''))", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            lstSystemName.Items.Clear();
            while (dr.Read())
            {
                lstSystemName.Items.Add(dr[0].ToString());
            }
            dr.Close();
            con.Close();
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
        ArrayList ar;
        string s = "''''";
        string a = "";
        DateTime lbr;
        string lbrS;
        string notes = "";
        SqlCommand cmd;
        private void btnGo_Click(object sender, EventArgs e)
        {
            SqlCommand cmd3 = new SqlCommand("select companyname from companymaster", con);
            con.Open();
            SqlDataReader dr = cmd3.ExecuteReader();
            while (dr.Read())
            {
                a = dr[0].ToString();
            }
            dr.Close();
            
            try
            {
                ar = new ArrayList();
                foreach (object selecteditem in lstSystemName.SelectedItems)
                {
                    ar.Add(selecteditem.ToString());
                    s = s + ",''" + selecteditem.ToString() + "'' ";
                    //Dis = Dis + "" + selecteditem.ToString() + ", ";
                }
                // FOR LAST_BATTERY_REPLACED_ON (START)
                //-------------------------------------
                SqlCommand cmdlbr = new SqlCommand("select max(convert(datetime,lastbatteryreplacedon,105)) from systemreport where location in (select locationname from locationmaster where districts =''"+cmbDistricts.Text+"'') and systemname in ("+s+")", con);
                SqlDataReader drlbr = cmdlbr.ExecuteReader();
                while (drlbr.Read())
                {
                    lbr =  Convert.ToDateTime (drlbr[0].ToString());  
                }
                drlbr.Close();
                System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
                dateInfo.ShortDatePattern = "dd-mm-yyyy";
                lbrS=lbr.ToString("dd-MM-yyyy");

                //FOR LAST_BATTERY_REPLACED_ON (END)
                
                // FOR PENDING POINTS OR NOTES (START)
                //-------------------------------------
                string notesco = "";
                string notescs = "";
                string remarks = "";
                SqlCommand cmdnotes = new SqlCommand("select remarks from complaintorder where location in (select locationname from locationmaster where districts =''" + cmbDistricts.Text + "'')and service in (" + s + ")", con);
                SqlDataReader drnotes = cmdnotes.ExecuteReader();
                while (drnotes.Read())
                {
                    notesco = drnotes[0].ToString();
                }
                drnotes.Close();

                SqlCommand cmdnotes2 = new SqlCommand("select cs.remarks from complaintstatus cs ,complaintorder co where co.location in (select locationname from locationmaster where districts =''" + cmbDistricts.Text + "'')and co.service in (" + s + ")and co.complaintno=cs.complaintno and cs.complaintstatus=''Open'')", con);
                SqlDataReader drnotes2 = cmdnotes2.ExecuteReader();
                while (drnotes2.Read())
                {
                    notescs = drnotes2[0].ToString();
                }
                drnotes2.Close();

                SqlCommand cmdnotes3 = new SqlCommand("select notes from systemreport where location in (select locationname from locationmaster where districts =''"+cmbDistricts.Text+"'')and systemname in ("+s+") and convert(datetime,date,105)=(select max(convert(datetime,date,105))from systemreport where location in (select locationname from locationmaster where districts =''"+cmbDistricts.Text+"'') and systemname in ("+s+"))", con);
                SqlDataReader drnotes3 = cmdnotes3.ExecuteReader();
                while (drnotes3.Read())
                {
                    notes = drnotes3[0].ToString();
                }
                drnotes3.Close();

                if (notesco.Length > 0)
                {
                  remarks = "";
                    remarks = notesco;
                }
                if (notescs.Length > 0)
                {
                    remarks = "";
                    remarks = notescs;
                }
                if (notes.Length > 0)
                {
                    remarks = "";
                    remarks = notes;
                }

                // FOR PENDING POINTS OR NOTES (STOP)
                
                cmd = new SqlCommand("(select distinct(co.customername) as CustomerName, co.complaintno as ComplaintNo,co.location as Location,lm.address as Address,lm.mobile as Mobile,co.service as System,co.systemmake as SystemMake,co.complaintdate as ComplaintDate,'''' as LastBatteryReplacedOn,'''',cs.complaintstatus as ComplaintStatus,defects as Defects from amc,complaintorder co,complaintstatus cs,locationmaster lm where co.complaintno=cs.complaintno and cs.complaintstatus=''Open'' and co.customername=lm.customername and lm.locationname=co.location and co.location in (select locationname from locationmaster where districts=''" + cmbDistricts.Text + "'') and co.service in (" + s + ") and convert(datetime,complaintdate,105) between convert(datetime,''" + dtpfromdate.Text + "'',105) and convert(datetime,''" + dtptodate.Text + "'',105)) union (select distinct(amc.customername),'''',amc.location,lm.address,lm.mobile,amc.system,amc.systemmake,amc.serviceduedates,'''','''','''','''' from amc amc, locationmaster lm where amc.location=lm.locationname and amc.location in (select locationname from locationmaster where districts=''" + cmbDistricts.Text + "'' and convert(datetime,serviceduedates,105) between convert(datetime,''" + dtpfromdate.Text + "'',105) and convert(datetime,''" + dtptodate.Text + "'',105))and amc.system in (" + s + ")  ) union (select distinct(ao.customername),'''',ao.location,lm.address,lm.mobile,ao.system,ao.systemmake,ao.serviceduedates,'''','''','''','''' from amcorder ao,locationmaster lm where ao.location=lm.locationname and ao.location in (select locationname from  locationmaster where districts=''" + cmbDistricts.Text + "'') and ao.system in (" + s + ") and convert(datetime,serviceduedates,105) between convert(datetime,''" + dtpfromdate.Text + "'',105) and convert(datetime,''" + dtptodate.Text + "'',105)) order by ComplaintNo desc ", con);
                

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "workorderForE");

                /* Last Battery Replaced On*/
                for (int row = 0; row < ds.Tables[0].Rows.Count; row++)
                {
                    SqlCommand cmdrow = new SqlCommand("select max(convert(datetime,lastbatteryreplacedon,105)) from systemreport where Customername=''" + ds.Tables[0].Rows[row][0].ToString() + "'' and location=''" + ds.Tables[0].Rows[row][2].ToString() + "'' and systemname=''" + ds.Tables[0].Rows[row][5].ToString() + "'' and makeofthesystem=''" + ds.Tables[0].Rows[row][6].ToString() + "''", con);
                    SqlDataReader drrow = cmdrow.ExecuteReader();
                    while (drrow.Read())
                    {
                        if (drrow[0].ToString() == "1/1/1900 12:00:00 AM")
                        {
                            ds.Tables[0].Rows[row][8] = null;
                        }
                        else
                        {
                            ds.Tables[0].Rows[row][8] = drrow[0].ToString();
                        }

                    }
                    drrow.Close();
                    //con.Close();
                }
                /* Last Battery Replaced On*/

                /* Remarks*/
                ds.WriteXmlSchema("../WorkOrderForD.xml");
                crwod = new CrystalReportForWorkOrderD();
                crwod.SetDataSource(ds.Tables[0]);
                //a = ConfigurationSettings.AppSettings.Get("companyname");
                crystalReportViewer1.ReportSource = crwod;
                s = "";
                crwod.SetParameterValue("district", cmbDistricts.Text);
                crwod.SetParameterValue("FromDate", dtpfromdate.Text);
                crwod.SetParameterValue("ToDate", dtptodate.Text);
                crwod.SetParameterValue("System", s);
                crwod.SetParameterValue("AllotedTo", cmbAllotedTo.Text);
                crwod.SetParameterValue("companyname", a);
                s = "''''";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
            
            con.Close();
        }
    }
}





谢谢您,





Thank you,

推荐答案


请更改为
convert.ToString(date);


问候,
shefeek
Hi,
please change to
convert.ToString(date);


regards,
shefeek


Convert.ToDateTime(TextBox1.Text)
Convert.ToDateTime(TextBox1.Text)


由于您未指明要在哪里获得例外,我会问一个不相关的问题.

听说过存储过程"吗? Google是您的朋友.
Since you didn''t indicate WHERE you''re getting the exception, I''ll ask an unrelated question.

Ever heard of "stored procedures"? Google is your friend.


这篇关于无法转换日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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