Solved

Use SQL data, C# and ASP.net to populate <title> <h1> and <div>

Posted on 2011-03-22
16
863 Views
Last Modified: 2012-05-11
I am having trouble finding any information on using C# and asp.net to extract data from a SQL database and using that data for the text in <title> <H1> <H2> <H3> and <div> tags. Is this not possible or am I not searching the right phrase.

Thanks
0
Comment
Question by:DotNetChano
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35194580
Hi,
there is no direct way,  but you can either use one of following
First. create html on fly
Second. if you have some static structure then make the items runat="server" and assign them id too. then you can add data in them
Third. Add Placeholders, and then add controls dyamically in them with data.

Use what ever you find convenient or suitable as per your requirements

Thanks
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35194992
the structure of the page will not change (i.e. the page will look the same from page to page), the only part that will change will be the title, the header tags and the content of the divs. Basically I will be storing my articles in sql and will write a querry to pull articleA based on the page name (www.mysite.com/articleA.aspx). Of the three methods you mentioned which would be the best route and what specific asp controls would i use? thanks for your help!
0
 
LVL 1

Expert Comment

by:smano
ID: 35195674
Is this is what you looking for?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Expert Comment

by:smano
ID: 35195677
Hmm, code didn't show up
CREATE TABLE TEMP (id int, htmlField varchar(max))

DECLARE @Value varchar(50)
DECLARE @HTML VARCHAR(MAX)

SET @Value = 'My Text goes here'
SET @HTML = '<div class="myclass">' + @Value + '</div'
INSERT INTO TEMP (id,htmlField)
VALUES(1,@HTML)

SELECT * FROM TEMP

Open in new window

0
 
LVL 1

Expert Comment

by:smano
ID: 35195683
missed '>' so line 7 in the end would be '</div>'
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35195744
Im not sure but I think you are showing me how to create a temp table for the db right there. I have that in sql already

Currently i have these fields:
UID
Title
H1
H2
Div1Data
Author
Date
URL (aka page name)


Here is what I was thinking I need, something along these lines anyways. Is what Im asking for even possible?

Thanks

0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35195756
code didnt show up for me either
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="articleA.aspx.cs" Inherits="ArticleA" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title> <%#("title" %> </title>
</head>
<body>
    <form id="form2" runat="server">

    <%#("H1")%>

    <%#("H2")%>

        <div id="Div1" >
        <%#("Div1Data")%>
        </div>

    </form>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DynamicDataConnectionString %>" 
        SelectCommand="SELECT [UID], [title], [H1], [H2], [Div1Data], [Author], [Date] FROM [dynamic] WHERE ([url] = @url)" 
    </asp:SqlDataSource>
</body>
</html>

Open in new window

0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 35198865
Hi

Yes, what you are asking is possible and the approach you are following should work.

The other approach you can use is the one IJZ described which probably needs an example. You'd write your tags like this:

<title id="title" runat="server" />

In your codebehind you'd then write

title.InnerHtml = "This is my title";

You can do the same in the body: <h1 id="myheading" runat="server"/>

myheading.InnerHtml = "This is my heading";

and so on with your h2 and your div.
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35200378
when i try to call myHeading from the codebehind i get error "The name 'myHeading' does not exist in the current context"

I am trying this code:

myHeading.InnerHtml = "Here is my H1 text"

________________________________________________________________________________

Also I used Page.Title to reference the title text

Like this:

Page.Title = "Article A, Keywords, keywords, etc";
0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 35201019
Can you upload your codebehind file please?

Also, is this the only page of this name in your site?  If you have more than one page referring to the same codebehind, the compiler gets confused.
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35201946
I was actually able to fix the H1 issue by adding the runat=Server to my H1 tag

But I do need help referencing a c# string in a sql querry if you know how to fix that?

the c# string is filenamespace

i will put the codebehind in here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public partial class Diamond_Ring : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Below cleans the filename up so that it is the raw file name with no underscores or .aspx

        string filename = Path.GetFileNameWithoutExtension(Request.PhysicalPath);
        string filenameclean = filename.Replace("_", " ");


        //Below pulls the record from the database based on the filenameclean variable

        SqlDataSource DynamicDataSource = new SqlDataSource();
        DynamicDataSource.ConnectionString = "ConnectionStrings:DynamicDataConnectionString";
        DynamicDataSource.SelectCommand = "SELECT [UID], [Title], [H1], [Div1], [filename], FROM [dynamic] WHERE ([filename] = filenameclean)";

        
        //Below binds my SqlData (Defined by []) to my html content (Defined by DivId.InnerHtml)
        //
        //The title will look like this on the aspx page <title divid=Title runat=Server></title>
        //The H1 will look like this on the aspx page <H1 divid=H1 runat=Server></title>
        //The Div1 will look like this on the aspx page <div divid=div1 runat=Server></title>

        Page.Title = [Title]
        H1.InnerHtml = [H1]
        Div1.InnerHtml = [Div1]


    }

}

Open in new window

0
 
LVL 1

Accepted Solution

by:
smano earned 500 total points
ID: 35203013
Follow my example since I duplicated the results:

ASPX page (I added the following):
<body>
    <form id="form2" runat="server">

<h1><asp:Label ID="lblH1" runat="server" /></h1><br />
<div><asp:Label ID="lblDiv1" runat="server" /></div>

</form>
CODE-BEHIND (added using System.Configuration to retrieve the connection string stored in web.config)


protected void Page_Load(object sender, EventArgs e)
    {
        DataSourceSelectArguments args = new DataSourceSelectArguments();

        string filename = "Whatever";
        string sql = "SELECT [H1], [DIV] FROM [dynamic] WHERE [filename] = '" + filename + "'";
        SqlDataSource source = 
            new SqlDataSource(ConfigurationManager.ConnectionStrings["DevelopmentConnection"].ConnectionString, sql);
        source.SelectCommandType = SqlDataSourceCommandType.Text;
        
        DataView view = (DataView)source.Select(args);
        DataTable table = view.ToTable();

        string H1 = table.Rows[0].ItemArray[0].ToString();
        string Div1 = table.Rows[0].ItemArray[1].ToString();
        lblH1.Text = H1;
        lblDiv1.Text = Div1;
     

    }

Open in new window

0
 
LVL 1

Expert Comment

by:smano
ID: 35203024
And it works just fine.
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35204375
worked perfectly

here are the classes or whatever their called that i used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
0
 
LVL 4

Author Comment

by:DotNetChano
ID: 35204381
thank you!
0
 
LVL 1

Expert Comment

by:smano
ID: 35204416
Glad to help.
For the database access I really suggest to utilize data access class library as a n-tier and enterprise library DATA block. That way you don't have to deal with sqldatasource and in better control.
Another words n-tier access :
UI calls BL (business tier) then to DA (data access) then back to UI with generic collection results formed through Entity object. Just a suggestion.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question