2

I am saving the inner Html in the form table , in the HTML field and getting the inner Html in Code Behind like following, and I want to achieve the Name Attribute(inner html has input types elements and each input type has a name attribute, i want to get those name attribute ) here in the code behind from the innerHTML . is this possible to do So

e.g < <input name="LastName" type="text" id="LastName">

C# method

protected void GetFormHTML()
{
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    cmd = new SqlCommand("select * from Forms where FormId='" + 
                         Request.QueryString["ID"].ToString() + "'", con);
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        dr.Read();

        lblFormName.Text = dr["Name"].ToString();
        DivHTML.InnerHtml = dr["HTML"].ToString().Trim();
    }

    dr.Close();
    cmd.Dispose();
    con.Close();
}
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280

4 Answers4

3

It sounds like you are attempting to parse HTML strings stored in the database. You need a library for that, e.g. HTML Agility Pack.

At least Convert.ToInt32(Request.QueryString["ID"].ToString()) or better yet, use ADO.NET parameters.

wp78de
  • 17,272
  • 6
  • 36
  • 68
MatthewMartin
  • 31,164
  • 31
  • 106
  • 162
0

Looks like this might have been answered before. Html 5 is not xml. So might need to confirm this library can work with that.

What is the best way to parse html in C#?

Community
  • 1
  • 1
bhiku
  • 81
  • 2
0

I don't know what type of SQL are you using, but must be attentive about this action:

if (dr.HasRows)
    {
        dr.Read();
        ...
    }

For example: SQLite's using a queue, and if has not made ​​full inquiry then in some future throw exception when you will want execute other query, but not made full inquiry before. Better way:

if(dr.HasRows)
{
   while(dr.Read())
   {
      ...
   }
}

or if you want only first item use LIMIT in sql query or by this way:

if(dr.HasRows)
{
    bool IsFirst = true;
    while(dr.Read())
    {
       if(IsFirst)
       {
           ...
          IsFirst = false;
       }
    }
}

but everytime use while(dr.Read()), not other way.

Marek Woźniak
  • 1,736
  • 15
  • 33
0

Instead of using standard HTML form input elements why not use ASP.NET controls. All you have to do is put the "runat=server" attribute into your existing form input elements and then you can reference everything you want from CodeBehind.

As a side note, there are MUCH, MUCH better ways to achieve what it looks like you are trying to do. I could rattle off a bunch right now. These are in no particular order and all deal with various aspects of what you need to accomplish. Meaning, these suggestions aren't so that you can pick 1 of them. These are a collection of programming and/or .NET framework solutions that each accomplish different things. You can use a combination of them or just one. Some you can use to just enhance what you've already done. Using LINQ instead of the inline SQL (or anything other than inline SQL for that matter) would be a good start.

  1. Databinding
  2. LINQ
  3. ORM - Entity Framework
  4. Replace you existing open/close connection with a "using" statement.

There are many other ways to do what you are trying to do. These are just a few concepts/ideas that you should definitely research before doing any more work on what you already have.

Ami Schreiber
  • 287
  • 2
  • 5
  • 20