“仅在服务器中”字符串未被识别为有效的DateTime“,但它在我的本地系统中正常工作

时间:2013-05-11 09:39:35

标签: c# sql-server datetime

这是使用c#将datetime插入sql server 2008的代码。这个相同的代码在我的本地系统中工作正常但有时在服务器上不起作用。请查看下面提到的代码并告诉我你的答案和建议

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

3 个答案:

答案 0 :(得分:1)

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

string expirydate = txtexpirydate.Text;

...与

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

同样适用于regdate

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

答案 1 :(得分:0)

而不是:

string regdate = txtregdate.Text;
string expirydate = txtexpirydate.Text;

试试这个:

Datetime regdate =  DateTime.ParseExact(txtregdate.Text, "M-d-yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime expirydate =  DateTime.ParseExact( txtexpirydate.Text, "M-d-yyyy", System.Globalization.CultureInfo.InvariantCulture);

假设您的文本框包含格式的日期:M-d-yyyy,否则更改它。

答案 2 :(得分:0)

避免这种情况的方法是设置参数的SqlDbType,并在分配参数值时, cast 将客户端值映射到映射到SqlDbType的CLR类型。