Je crée une application Web J'utilise un textbox et un extendeur de textbox pour get la date saisie par l'user, je veux Date dans ce format ("dd-MM-yyyy")
,
mais je reçois une erreur
(Erreur de dépassement arithmétique lors de la conversion de l'expression en type de données datetime.)
Par défaut, le format de la date est ("yyyy-MM-dd")
protected void txttodate_TextChanged(object sender, EventArgs e) { ssortingng com = Convert.ToSsortingng(Session["radio"]); ssortingng com1 = Convert.ToSsortingng(Session["mysession"]); Session["drd3"] = com.ToSsortingng(); Session["lblname"] = com1.ToSsortingng(); SqlCommand cmdtr = new SqlCommand("select empname from trainerdetails where sortingd='"+com1.ToSsortingng()+"'",con); con.Open(); SqlDataReader dr = cmdtr.ExecuteReader(); while (dr.Read()) { lblempname.Text = dr["empname"].ToSsortingng(); } GridView1.Visible = true; connectionstr = WebConfigurationManager.ConnectionSsortingngs["Constr"].ConnectionSsortingng; con = new SqlConnection(connectionstr); SqlCommand cmd = new SqlCommand("csuvdaterange"); cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = con; SqlParameter[] param = { new SqlParameter("@logintype",com.ToSsortingng()), new SqlParameter("@name",lblempname.Text), new SqlParameter("@datefrm",txtfrmdate.Text), new SqlParameter("@dateto",txttodate.Text) }; cmd.Parameters.AddRange(param); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); }
Voici comment mon code backend
<asp:TextBox ID="txtfrmdate" Visible="false" runat="server" placeholder="From" style="margin-left:10px; margin-top:8px;" Height="30px" Width="200px"></asp:TextBox> <asp:CalendarExtender ID="calenderfromextend" TargetControlID="txtfrmdate" runat="server" Format="dd-MM-yyyy"></asp:CalendarExtender> <asp:TextBox ID="txttodate" placeholder="To" Visible="false" runat="server" style="margin-left:10px; margin-top:8px;" AutoPostBack="true" OnTextChanged="txttodate_TextChanged" Height="30px" Width="200px"></asp:TextBox> <asp:CalendarExtender ID="calendertoextend" TargetControlID="txttodate" runat="server" Format="dd-MM-yyyy"></asp:CalendarExtender>
Mes zones de text et l'extension de calendar
Après avoir implémenté les suggestions de Jon sur l'utilisation de SQL paramétré , en using
instructions et si vous êtes sûr que les dates saisies dans TextBox
sont au format " yyyy-MM-dd
", vous pouvez utiliser ce code pour changer la string de format:
SqlParameter[] param = { new SqlParameter("@logintype",com.ToSsortingng()), new SqlParameter("@name",lblempname.Text), new SqlParameter("@datefrm",DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToSsortingng("yyyy-MM-dd")), new SqlParameter("@dateto",DateTime.ParseExact(txttodate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToSsortingng("yyyy-MM-dd")) };
Commencez par parsingr votre textbox à un object DateTime.ParseExact
par DateTime.ParseExact
var dateFrom = DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", null); var dateTo = DateTime.ParseExact(txttodate.Text, "dd-MM-yyyy", null);
Après cela, remplacez dans votre SqlParameter
:
SqlParameter[] param = { new SqlParameter("@logintype",com.ToSsortingng()), new SqlParameter("@name",lblempname.Text), new SqlParameter("@datefrm",SqlDbType.DateTime) { Value = dateFrom }, new SqlParameter("@dateto", SqlDbType.DateTime) { Value = dateTo } };
Ssortingng.Format("{0:MM/dd/yyyy}", dt);