Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
protected void OnDelete(object sender, EventArgs e)
   {
       //Find the reference of the Repeater Item.
       RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;
       int orderId = int.Parse((item.FindControl("lblOrderID") as Label).Text);
       string custName = (item.FindControl("txtCustName") as TextBox).Text.Trim();
       string custPhone = (item.FindControl("txtPhone") as TextBox).Text.Trim();
       string custEmail = (item.FindControl("txtEmail") as TextBox).Text.Trim();
       string date = (item.FindControl("txtDate") as TextBox).Text.Trim();
       string cartAmount = (item.FindControl("txtCartAmount") as TextBox).Text.Trim();
       string cartDiscount = (item.FindControl("txtCartDiscount") as TextBox).Text.Trim();
       string totalPaid = (item.FindControl("txtTotalPaid") as TextBox).Text.Trim();
       string orderNumber = (item.FindControl("txtOrderNumber") as TextBox).Text.Trim();
       string orderStatus = (item.FindControl("txtOrderStatus") as TextBox).Text.Trim();




       string constr = ConfigurationManager.ConnectionStrings["KalzAgency"].ConnectionString;
       using (SqlConnection con = new SqlConnection(constr))
       {
           using (SqlCommand cmd = new SqlCommand("tblOrders_CRUD"))
           {
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@Action", "DELETE");
               cmd.Parameters.AddWithValue("@OrderID", orderId);
               cmd.Parameters.AddWithValue("@Name", custName);
               cmd.Parameters.AddWithValue("@MobileNumber", custPhone);
               cmd.Parameters.AddWithValue("@Email", custEmail);
               cmd.Parameters.AddWithValue("@DateOfPurchase", date);
               cmd.Parameters.AddWithValue("@CartAmount", cartAmount);
               cmd.Parameters.AddWithValue("@CartDiscount", cartDiscount);
               cmd.Parameters.AddWithValue("@TotalPaid", totalPaid);
               cmd.Parameters.AddWithValue("@OrderNumber", orderNumber);
               cmd.Parameters.AddWithValue("@OrderStatus", orderStatus);
               cmd.Connection = con;
               con.Open();
               cmd.ExecuteNonQuery();
               con.Close();
           }
       }
       this.BindCategoryReapter();
   }


What I have tried:

I set the data type in database with datetime for the date. When i click delete, the error occured. How can i solve this?
Posted
Updated 6-Apr-21 9:15am
v2

1 solution

You are passing a string to the @DateOfPurchase parameter. That string does not contain a valid date.

Use DateTime.TryParse[^] to attempt to convert the string to a date. If it succeeds, pass the parsed DateTime value to the parameter. If it fails, display an error to the user and abort the operation.
 
Share this answer
 
Comments
Maciej Los 7-Apr-21 3:33am    
5ed!
Member 15100384 13-Apr-21 13:17pm    
where should i put it?
Richard Deeming 14-Apr-21 4:24am    
Replace:
string date = (item.FindControl("txtDate") as TextBox).Text.Trim();

with:
DateTime date;
if (!DateTime.TryParse((item.FindControl("txtDate") as TextBox).Text.Trim(), out date))
{
    ... DISPLAY ERROR ...
    return;
}
Member 15100384 15-Apr-21 3:51am    
I got the same error, "Error converting data type nvarchar to datetime."
Richard Deeming 15-Apr-21 4:41am    
Then there's something else wrong with your database which you haven't shown in your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900