Monday, March 26, 2012

The conversion of a char data type to a datetime data type resulted in an out-of-range ...

Hi,
I couldn't understand why I can′t convert the date. I use dd/mm/yyyy, and I put the correct sintax in my code:

CType(tbVencimento.Text,Date)
but it conti nues recording inside of my database like mm/dd/yyyy, why?
and every moment I receive this error message:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

Please, help!
Thanks a lot!
Raphael Amador

You should use DateTime.ParseExact to get the format changed.
Dim s As String
s = "25/12/2005"
Dim d As DateTime
d = DateTime.ParseExact(s, "dd/MM/yyyy", Nothing)
Then when it is inserted into the Sql, since it is coming from a DateTime object, the format will be correct and you won't get the out-of-range value error. And it will show as mm/dd/yyyy format as 12/25/2005.
bill
Hi, I tried:

<%@dotnet.itags.org.PageLanguage="VB" %>

<%@dotnet.itags.org.ImportNamespace="System.Data" %>

<%@dotnet.itags.org.ImportNamespace="System.Data.SQLClient" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<scriptrunat="server">

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

tbLiquido.Text =CType(tbTotNF.Text,Decimal) -CType(tbDesconto.Text,Decimal)

lblIData.Text =CDate(Today)

EndSub


ProtectedSub ddlClientes_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim SQLAsString ="Select CODCLI, RAZAO, ENDERECO, ESTADO FROM CLIENTES WHERE CODCLI = '" & ddlClientes.SelectedItem.Value &"'"

Dim Conex?oAs SqlConnection =New SqlConnection("Data Source=SERV-IPE2;Initial Catalog=IpeSys;Persist Security Info=True;User ID=ASPNET;Password=IpeSys")

Dim ComandoSQLAsNew SqlCommand(SQL, Conex?o)

Conex?o.Open()

Dim drAs SqlDataReader

dr = ComandoSQL.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

dr.Read()

tbENDERECO.Text = dr("ENDERECO")

tbESTADO.Text = dr("ESTADO")

Conex?o.Close()

EndSub


ProtectedSub Gravar_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim SQLAsString ="Insert into RECEBER(NFNUMERO_receber, EMPRESA_receber, TIPOFAT_receber, EMISSAO_receber, CODCLI_receber, CLIENTE_receber, ENDERECO_receber, ESTADO_receber, TOTALNOTA_receber, VENCIMENTO_receber, BANCO_receber, CCdoBANCO_receber, DESCONTO_receber, LIQUIDO_receber, SALDOREC_receber,IUSER_receber, IDATE_receber) values (" &CType(tbNOTAFISCAL.Text,Decimal) &",'" & ddlEmpresa.SelectedValue &"','" & rblTipoFat.SelectedValue &"','" &CType(tbDataEmissao.Text, DateTime) &"','" & ddlClientes.SelectedItem.Value &"','" & ddlClientes.SelectedItem.Text &"','" & tbENDERECO.Text &"','" & tbESTADO.Text &"','" &CType(tbTotNF.Text,Decimal) &"','" &CType(tbVencimento.Text, DateTime) &"','" & ddlBancoCob.SelectedItem.Text &"','" & ddlBancoCCorrente.SelectedItem.Text &"','" &CType(tbDesconto.Text,Decimal) &"','" &CType(tbLiquido.Text,Decimal) &"','" &CType(tbLiquido.Text,Decimal) &"','" & lblIUSER.Text &"','" &CType(lblIData.Text,Date) &"')"

Dim Conex?oAs SqlConnection =New SqlConnection("Data Source=SERV-IPE2;Initial Catalog=IpeSys;Persist Security Info=True;User ID=ASPNET;Password=IpeSys")

Dim ComandoSqlAsNew SqlCommand(SQL, Conex?o)

Conex?o.Open()

Dim sAsString

s ="25/12/2005"

Dim dAs DateTime

d = DateTime.ParseExact(s,"dd/MM/yyyy",Nothing)

ComandoSql.ExecuteNonQuery()

Conex?o.Close()

Response.Redirect("Lista.aspx")

EndSub


ProtectedSub CancelButton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

Response.Redirect("Lista.aspx")

EndSub


But I have the same problem with the out-of-range error. Can you explain me more about your code??
Thanks a lot.
Raphael


Hi, I tried:

<%@dotnet.itags.org.PageLanguage="VB" %>

<%@dotnet.itags.org.ImportNamespace="System.Data" %>

<%@dotnet.itags.org.ImportNamespace="System.Data.SQLClient" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<scriptrunat="server">

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

tbLiquido.Text =CType(tbTotNF.Text,Decimal) -CType(tbDesconto.Text,Decimal)

lblIData.Text =CDate(Today)

EndSub


ProtectedSub ddlClientes_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim SQLAsString ="Select CODCLI, RAZAO, ENDERECO, ESTADO FROM CLIENTES WHERE CODCLI = '" & ddlClientes.SelectedItem.Value &"'"

Dim Conex?oAs SqlConnection =New SqlConnection("Data Source=SERV-IPE2;Initial Catalog=IpeSys;Persist Security Info=True;User ID=ASPNET;Password=IpeSys")

Dim ComandoSQLAsNew SqlCommand(SQL, Conex?o)

Conex?o.Open()

Dim drAs SqlDataReader

dr = ComandoSQL.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

dr.Read()

tbENDERECO.Text = dr("ENDERECO")

tbESTADO.Text = dr("ESTADO")

Conex?o.Close()

EndSub


ProtectedSub Gravar_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim SQLAsString ="Insert into RECEBER(NFNUMERO_receber, EMPRESA_receber, TIPOFAT_receber, EMISSAO_receber, CODCLI_receber, CLIENTE_receber, ENDERECO_receber, ESTADO_receber, TOTALNOTA_receber, VENCIMENTO_receber, BANCO_receber, CCdoBANCO_receber, DESCONTO_receber, LIQUIDO_receber, SALDOREC_receber,IUSER_receber, IDATE_receber) values (" &CType(tbNOTAFISCAL.Text,Decimal) &",'" & ddlEmpresa.SelectedValue &"','" & rblTipoFat.SelectedValue &"','" &CType(tbDataEmissao.Text, DateTime) &"','" & ddlClientes.SelectedItem.Value &"','" & ddlClientes.SelectedItem.Text &"','" & tbENDERECO.Text &"','" & tbESTADO.Text &"','" &CType(tbTotNF.Text,Decimal) &"','" &CType(tbVencimento.Text, DateTime) &"','" & ddlBancoCob.SelectedItem.Text &"','" & ddlBancoCCorrente.SelectedItem.Text &"','" &CType(tbDesconto.Text,Decimal) &"','" &CType(tbLiquido.Text,Decimal) &"','" &CType(tbLiquido.Text,Decimal) &"','" & lblIUSER.Text &"','" &CType(lblIData.Text,Date) &"')"

Dim Conex?oAs SqlConnection =New SqlConnection("Data Source=SERV-IPE2;Initial Catalog=IpeSys;Persist Security Info=True;User ID=ASPNET;Password=IpeSys")

Dim ComandoSqlAsNew SqlCommand(SQL, Conex?o)

Conex?o.Open()

Dim sAsString

s ="25/12/2005"

Dim dAs DateTime

d = DateTime.ParseExact(s,"dd/MM/yyyy",Nothing)

ComandoSql.ExecuteNonQuery()

Conex?o.Close()

Response.Redirect("Lista.aspx")

EndSub


ProtectedSub CancelButton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

Response.Redirect("Lista.aspx")

EndSub


But I have the same problem with the out-of-range error. Can you explain me more about your code??
Thanks a lot.
Raphael

0 comments:

Post a Comment