OPMUANRK OPMUANRK - 4 months ago 18
ASP.NET (C#) Question

Validate GridView_RowUpdating textfields ASP.NET

I would like to have validations on my Gridview when I'm editing some information on it, but I don't know how. I'm not using the simple way, I'm doing it by code (C#), and I don't know how to add validations on my textfields...

Here's some of my code:

protected void gvAdmins_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["TaskTableA"];

GridViewRow row = gvAdmins.Rows[e.RowIndex];

dt.Rows[row.DataItemIndex]["ID"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["H:"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["M:"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
dt.Rows[row.DataItemIndex][6] = ((TextBox)(row.Cells[7].Controls[0])).Text;
dt.Rows[row.DataItemIndex][7] = ((TextBox)(row.Cells[8].Controls[0])).Text;
dt.Rows[row.DataItemIndex][8] = ((TextBox)(row.Cells[9].Controls[0])).Text;
dt.Rows[row.DataItemIndex][9] = ((TextBox)(row.Cells[10].Controls[0])).Text;
dt.Rows[row.DataItemIndex][10] = ((TextBox)(row.Cells[11].Controls[0])).Text;

gvAdmins.EditIndex = -1;

int id = Convert.ToInt32(Request.QueryString["id"]);
string idCar = ((TextBox)(row.Cells[1].Controls[0])).Text;
int hom = Convert.ToInt32(((TextBox)(row.Cells[4].Controls[0])).Text);
int mu = Convert.ToInt32(((TextBox)(row.Cells[5].Controls[0])).Text);
int t1 = 0;
int t2 = 0;
int t3 = 0;
int t4 = 0;
int t5 = 0;
t1 = Convert.ToInt32(((TextBox)(row.Cells[7].Controls[0])).Text);
t2 = Convert.ToInt32(((TextBox)(row.Cells[8].Controls[0])).Text);
t3 = Convert.ToInt32(((TextBox)(row.Cells[9].Controls[0])).Text);
t4 = Convert.ToInt32(((TextBox)(row.Cells[10].Controls[0])).Text);
t5 = Convert.ToInt32(((TextBox)(row.Cells[11].Controls[0])).Text);

int total = hom + mu;
int totalT = t1 + t2 + t3 + t4 + t5;

string comandoIF = //MY SQL Command;

conn.IAE(comandoIF);

dt.Rows[row.DataItemIndex]["Total:"] = total.ToString();

BindDataA();
}


I hope you could help me, thanks.

Answer

You should start by putting the contents of the TextBoxes or other elements that are being processed in gvAdmins_RowUpdating into variables and validate those.

For a string you can do this:

    string H = ((TextBox)(row.Cells[4].Controls[0])).Text;
    if (H == "correctValue")
    {
        dt.Rows[row.DataItemIndex]["H:"] = H;
    }
    else
    {
        //the input was not correct
    }

For values that need converting like integers, dates, booleans etc you do something like this. You have to convert the value from the TextBox (which is always a string).

    int ID = Convert.ToInt32(((TextBox)(row.Cells[1].Controls[0])).Text);
    if (ID > 500 && ID < 600)
    {
        dt.Rows[row.DataItemIndex]["ID"] = ID;
    }
    else
    {
        //the input was not correct
    }

But there's a catch, if the value from the TextBox cannot be converted to an integer your code will trow an error. To prevent that you can use a Try-Catch Block:

    int ID = -1;
    //try the conversion in a try-catch block. If conversion fails your site won't trow an error
    try
    {
        ID = Convert.ToInt32(((TextBox)(row.Cells[1].Controls[0])).Text);
    }
    catch
    {
        //error converting to integer, but will continue
    }

    //after that do the validating
    if (ID > 500 && ID < 600)
    {
        dt.Rows[row.DataItemIndex]["ID"] = ID;
    }
    else
    {
        //the input was not correct
    }

Note that the above examples are very basic. You could make validation very complex and strict if you wanted to. You should find a balance between getting usable validated data in the database and user experience etc.

I also recommend you ALSO use client side validation in the form of validators. These links can get you started on that:

https://msdn.microsoft.com/en-us/library/ms972961.aspx

https://msdn.microsoft.com/en-us/library/a0z2h4sw.aspx