0

i dont have an edit template. is it possible for me to add validations on my text box in grid view during edit mode?

It updates the edited fields and works fine, but when I type in special characters, it is still being accepted. How can I validate those editableTextBoxes and prevent the user from entering invalid input?

UPDATE

int prodid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString());
        string strprodname = ((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        //string strdesc = ((TextBox)gdview.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string strprice = ((TextBox)gdview.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        //string strimg = ((TextBox)gdview.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        //string strquant = ((TextBox)gdview.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        var regex = new Regex(@"^\d{0,8}(\.\d{1,4})?$");

if (regex.IsMatch(strprice))
        {
            SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            conn.Open();
            da.UpdateCommand = new SqlCommand("update Products set Name='" + strprodname + "', Price ='" + strprice + "' where ProductID =" + prodid, conn);
            da.UpdateCommand.ExecuteNonQuery();
            conn.Close();
            gdview.EditIndex = -1;
            GetProducts(0);
        }

fields not updating

UPDATE getproducts(0)

private void GetProducts(int CategoryID)
    {
        ShoppingCart k = new ShoppingCart()
        {
            CategoryID = CategoryID
        };
        gdview.DataSource = null;
        gdview.DataSource = k.GetAllProducts();
        gdview.DataBind();
    }

datatable:

public DataTable GetAllProducts()
    {
        SqlParameter[] parameters = new SqlParameter[1];
        parameters[0] = DataLayer.DataAccess.AddParameter("@CategoryID", CategoryID, System.Data.SqlDbType.Int, 20);
        DataTable dt = DataLayer.DataAccess.ExecuteDTByProcedure("SP_GetAllProducts", parameters);
        return dt;
    }

UPDATE:

error on regex

Current Code:

protected void gdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int prodid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString());
        string strprodname = ((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        //string strdesc = ((TextBox)gdview.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string strprice = ((TextBox)gdview.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        //string strimg = ((TextBox)gdview.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        //string strquant = ((TextBox)gdview.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        var regex = new Regex(@"^\d{0,8}(\.\d{1,4})?$");

        if (regex.IsMatch(strprodname) && regex.IsMatch(strprice))
        {
            SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            conn.Open();
            da.UpdateCommand = new SqlCommand("update Products set Name='" + strprodname + "', Price ='" + strprice + "' where ProductID =" + prodid, conn);
            da.UpdateCommand.ExecuteNonQuery();
            conn.Close();
            gdview.EditIndex = -1;

        }
        GetProducts(0);
    }
Reine Viray
  • 65
  • 2
  • 11

2 Answers2

0

Visit http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/validation/defaultcs.aspx

you may get the solution.

Vke
  • 58
  • 1
  • 13
0
var regex_valid_price = new Regex(@"^\d{0,8}(\.\d{1,4})?$");
var regex_no_special_chars = new Regex(@"^[a-zA-Z0-9 ]*$");

if(regex_no_special_chars.IsMatch(strprodname) && regex_valid_price.IsMatch(strprice)){
    // do your db inserts
}
mattygee
  • 147
  • 6
  • read up on what regular expressions do here: http://www.regular-expressions.info/ and test them here: https://regex101.com/ – mattygee Jun 15 '16 at 16:12
  • you should have just edited your other answer instead of making a new one. – AzNjoE Jun 15 '16 at 16:16