“字符串未被识别为有效的日期时间”在服务器,但它在我的本地系统工作正常 [英] "String was not recognized as a valid DateTime" in server only but it is working fine in my local system

查看:109
本文介绍了“字符串未被识别为有效的日期时间”在服务器,但它在我的本地系统工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用c#将datetime插入到sql server 2008中的代码。这个相同的代码在我的本地系统中工作正常,但是它在服务器上不能工作。请看下面提到的代码,让我知道你的答案和建议。

  protected void Button1_Click(object sender,EventArgs e)
{
string ad;
btnupdate.Visible = false;
btnactive1.Visible = false;
if(FileUpload1.HasFile)
{
//获取上传文件的长度
int length = FileUpload1.PostedFile.ContentLength;
//创建一个字节数组来存储二进制图像数据
byte [] imgbyte = new byte [length];
//存储当前选择的文件
HttpPostedFile img = FileUpload1.PostedFile;
//设置二进制数据
string area = ddlarea.SelectedItem.Text;
string cat = ddlcategory.SelectedItem.Text;
string subcat = ddlsubcategory.SelectedItem.Text;
string head = txthead.Text;
string subhead = txtsubhead.Text;
string shortdesc = txtshortdesc.Text;
string url = txturl.Text;
// string imagename = txtimagename.Text;
string regdate = txtregdate.Text;
string expirydate = txtexpirydate.Text;
string customername = txtcustomername.Text;
string contact = txtcontact.Text;
string email = txtemail.Text;
if(CheckBox2.Checked == true)
{
ad =付费广告;
}
else
{
ad =免费广告;
}
img.InputStream.Read(imgbyte,0,length);
con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand(insert into tbl_paid_ads(area,catagory,subcatagory,heading,subheading,shortdescription,url,imagedata,dateofregistration,dateofexpiry,customername,contactno,email,ad)values(@ area,@ cat ,@ subcat,@ head,@ subhead,@ shortdesc,@ url,@ imagedata,@ regdate,@ expirydate,@ customername,@ contact,@ email,@ ad),con);
// SqlCommand cmd = new SqlCommand(insert into tbl_paid_ads(image_name,image)values(@ imagename,@ imagedata),con);
cmd.Parameters.Add(@ area,SqlDbType.NVarChar,50).Value = area;
cmd.Parameters.Add(@ cat,SqlDbType.NVarChar,50).Value = cat;
cmd.Parameters.Add(@ subcat,SqlDbType.NVarChar,50).Value = subcat;
cmd.Parameters.Add(@ head,SqlDbType.NVarChar,50).Value = head;
cmd.Parameters.Add(@ subhead,SqlDbType.NVarChar,100).Value = subhead;
cmd.Parameters.Add(@ shortdesc,SqlDbType.NVarChar,1000).Value = shortdesc;
cmd.Parameters.Add(@ url,SqlDbType.NVarChar,50).Value = url;
//cmd.Parameters.Add(\"@imagename,SqlDbType.VarChar,50).Value = imagename;
cmd.Parameters.Add(@ imagedata,SqlDbType.Image).Value = imgbyte;
cmd.Parameters.Add(@ regdate,SqlDbType.DateTime).Value = regdate;
cmd.Parameters.Add(@ expirydate,SqlDbType.DateTime).Value = expirydate;
cmd.Parameters.Add(@ customername,SqlDbType.NVarChar,50).Value = customername;
cmd.Parameters.Add(@ contact,SqlDbType.NVarChar,50).Value = contact;
cmd.Parameters.Add(@ email,SqlDbType.NVarChar,50).Value = email;
cmd.Parameters.Add(@ ad,SqlDbType.NVarChar,50).Value = ad;
int count = cmd.ExecuteNonQuery();
con.Close();
if(count == 1)
{
Response.Write(< script> alert('Paid Ad added successfully')< / script>);
}
resetpaidad();
}
}


解决方案

用户界面(UI)应该验证日期是否为特定格式,那么您需要将字符串转换为 DateTime 。例如,如果UI日期格式为 DD / MM / YYYY ,则应替换此行...

  string expirydate = txtexpirydate.Text; 

with ...

  DateTime expirydate = DateTime.ParseExact(txtexpirydate.Text,dd / MM / yyyy,
CultureInfo.InvariantCulture);

regdate 相同。 p>

注意:如果文本框的日期格式始终是固定的,上述将会起作用。但是,如果UI验证在不同的位置实施不同的格式,则需要获取UI当前使用的日期格式,这可能取决于用户界面运行的区域设置。


This is the code to insert datetime into sql server 2008 using c#. This same code is working fine in my local system but it is not working in server some times.please see the code mentioned below and let me know your answers and suggestions

protected void Button1_Click(object sender, EventArgs e)
{
    string ad;
    btnupdate.Visible = false;
    btnactive1.Visible = false;
    if (FileUpload1.HasFile)
    {
        //getting length of uploaded file
        int length = FileUpload1.PostedFile.ContentLength;
        //create a byte array to store the binary image data
        byte[] imgbyte = new byte[length];
        //store the currently selected file in memeory
        HttpPostedFile img = FileUpload1.PostedFile;
        //set the binary data
        string area = ddlarea.SelectedItem.Text;
        string cat = ddlcategory.SelectedItem.Text;
        string subcat = ddlsubcategory.SelectedItem.Text;
        string head = txthead.Text;
        string subhead = txtsubhead.Text;
        string shortdesc = txtshortdesc.Text;
        string url = txturl.Text;
        //string imagename = txtimagename.Text;
        string regdate = txtregdate.Text;
        string expirydate = txtexpirydate.Text;
        string customername = txtcustomername.Text;
        string contact = txtcontact.Text;
        string email = txtemail.Text;
        if (CheckBox2.Checked == true)
        {
            ad = "paid ad";
        }
        else
        {
            ad = "free ad";
        }
        img.InputStream.Read(imgbyte, 0, length);
        con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tbl_paid_ads (area,catagory,subcatagory,heading,subheading,shortdescription,url,imagedata,dateofregistration,dateofexpiry,customername,contactno,email,ad) values (@area,@cat,@subcat,@head,@subhead,@shortdesc,@url,@imagedata,@regdate,@expirydate,@customername,@contact,@email,@ad)", con);
        //SqlCommand cmd = new SqlCommand("insert into tbl_paid_ads (image_name,image) values (@imagename,@imagedata)", con);
        cmd.Parameters.Add("@area", SqlDbType.NVarChar, 50).Value = area;
        cmd.Parameters.Add("@cat", SqlDbType.NVarChar, 50).Value = cat;
        cmd.Parameters.Add("@subcat", SqlDbType.NVarChar, 50).Value = subcat;
        cmd.Parameters.Add("@head", SqlDbType.NVarChar, 50).Value = head;
        cmd.Parameters.Add("@subhead", SqlDbType.NVarChar, 100).Value = subhead;
        cmd.Parameters.Add("@shortdesc", SqlDbType.NVarChar, 1000).Value = shortdesc;
        cmd.Parameters.Add("@url", SqlDbType.NVarChar, 50).Value = url;
        //cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
        cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
        cmd.Parameters.Add("@regdate", SqlDbType.DateTime).Value = regdate;
        cmd.Parameters.Add("@expirydate", SqlDbType.DateTime).Value = expirydate;
        cmd.Parameters.Add("@customername", SqlDbType.NVarChar, 50).Value = customername;
        cmd.Parameters.Add("@contact", SqlDbType.NVarChar, 50).Value = contact;
        cmd.Parameters.Add("@email", SqlDbType.NVarChar, 50).Value = email;
        cmd.Parameters.Add("@ad", SqlDbType.NVarChar, 50).Value = ad;
        int count = cmd.ExecuteNonQuery();
        con.Close();
        if (count == 1)
        {
            Response.Write("<script>alert('Paid Ad added successfully')</script>");
        }
        resetpaidad();
    }
}

解决方案

The user interface (UI) should be validating that the date is in a specific format, then you need to convert the string to a DateTime. For example, if the UI date format is DD/MM/YYYY you should replace this line...

string expirydate = txtexpirydate.Text;

with...

DateTime expirydate = DateTime.ParseExact(txtexpirydate.Text, "dd/MM/yyyy", 
                                          CultureInfo.InvariantCulture);

And the same for regdate.

Note: the above will work if the date format of the text box is always fixed. But if the UI validation enforces different formats in different locations you will need to get the date format currently used by the UI which may be dependent on the locale where the UI is running.

这篇关于“字符串未被识别为有效的日期时间”在服务器,但它在我的本地系统工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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