Hi everybody,
Sub Validate_Data (Src As Object, Args As GridviewUpdaeEventArgs )

If Not IsNumeric(Args.NewValues("BookPrice")) Then
Args.Cancel = True
EditMSG.Text = "-- Book Price is not numeric. Record not updated." End If

How to write this if part in C#.

Please give the answer.

Thank you,
Aravind .

Dani AI

Generated

Good call by for switching to a TryParse-style check. Below are practical improvements and alternatives to make that pattern safer and more user-friendly in real apps.

A few points to follow:

  • Prefer a numeric type suited to money (decimal) rather than double when validating prices.
  • Always guard against missing or null values from e.NewValues before calling ToString() to avoid NullReferenceExceptions.
  • Use the TryParse overload that accepts NumberStyles and CultureInfo if you need to accept currency symbols or locale-specific separators. See the docs for Decimal.TryParse and NumberStyles.
  • For better UX, validate on the client with ASP.NET validators (Compare/Range/RegularExpression) and also validate on the server as a fallback; see the GridView RowUpdating event docs: GridView.RowUpdating Event.

Example workflow to handle the update safely:

  • Check e.NewValues.Contains("BookPrice") and that the value is not null/DBNull.
  • Trim the input and attempt decimal.TryParse with appropriate NumberStyles and CultureInfo.
  • If parsing fails, set e.Cancel = true and show a clear message. If it succeeds, consider writing the parsed decimal back into e.NewValues so the data layer receives a numeric type.

Common gotchas:

  • Inputs with currency symbols, commas, or different decimal separators will fail unless you allow them via NumberStyles/CultureInfo.
  • If values come from bound controls, e.NewValues might contain DBNull.Value — check for that.
  • Client-side validators prevent round-trips; server-side checks are required for security.

This preserves the basic approach you posted while making it robust for production scenarios.

Recommended Answers

All 4 Replies

Have you even tried? It looks like all your posts are just asking us to do work for you. Show some effort please.

errr... you should at least google it first, and then ask question once you tried it and didn't work XD

but basically you need to learn how to use class in C#, it's similar to Java :D

Hi guys,
Sorry actually I am beginner in .Net technologies.Thats why I am facing problems.Any how I found the solution .And here is the solution hoping it will be useful for someone.

Please give the alternatives if any

---Updating_Row(object sender, GridViewUpdateEventArgs e)
{
string str = e.NewValues["quantity"].ToString();
        double NUM;
        bool isNum = double.TryParse(str, out NUM);
        if (isNum==false)
        {
            e.Cancel = true;
            EditMsg.Text = "-- Book Quantity is not numeric. Record not updated";

        }
}

Thank you,
Aravind

hi arvind.

if u want to convert the code vb to c# and wise versa then the following link will definately helpful to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.