Search code examples
c#asp.netsqlparameter

String was not recognized when i use datetime.parseexact()


When ever i tries to enter date into sqlparameter it keeps on popping string was not recognized , i even tried to use ParseExact() but its still showing an error.

My .aspx :

<asp:TextBox ID="txtfromdate" runat="server" Width="90px" Height="20px">
</asp:TextBox>
<cc1:CalendarExtender ID="calender1" runat="server" 
                      TargetControlID="txtfromdate" 
                      Format="dd/MM/yyyy">

aspx.cs :

SqlParameter[] prm = {      
    new SqlParameter("@ContractName",SqlDbType.NVarChar){Value=txtContractname.Text.Trim()},
    new SqlParameter("@FromDate",DateTime.ParseExact(txtfromdate.Text,"dd/MM/yyyy",null)),
    new SqlParameter("@ToDate",DateTime.ParseExact(txtToDate.Text,"dd/MM/yyyy",null)) ,
    new SqlParameter("@VenID",SqlDbType.Int){Value=Convert.ToInt32(ddlsupplier.SelectedValue)} ,
    new SqlParameter("@CreatedBy",SqlDbType.Int){Value=Convert.ToInt32(Session["UserID"])}, //chng
    new SqlParameter("@OrgID",SqlDbType.Int){Value=Convert.ToInt32(Session["orgID"])},
    new SqlParameter("@Termsnconditions",SqlDbType.NVarChar){Value=txtTermsandconditions.Text} ,
    new SqlParameter("@Module",SqlDbType.NVarChar){Value=ddlModule.SelectedValue} ,
    new SqlParameter("@Filename",SqlDbType.NVarChar){Value=ddlAttachment.SelectedValue} ,
    new SqlParameter("@Currency",SqlDbType.NVarChar){Value=ddlCurrency.SelectedItem.Text} ,
    new SqlParameter("@ContractNumber",SqlDbType.NVarChar){Value =txtContractNumber.Text.Trim()} 
   };

Solution

  • I suspect the problem is the culture installed on the server; / in a custom DateTime specifier doesn't actually represent / - it represents the DateSeparator. If you actually want it to use /, the simplest way to do that is to explicitly specify the invariant culture; try simply replacing the null (in ParseExact) with CultureInfo.InvariantCulture. If that still doesn't work, then I strongly suspect that the incoming value is not quite 12/12/2012 - it could, for example, have whitespace or unicode hidden characters in it.