Solved

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

Posted on 2011-03-22
16
836 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image(2) 3 28
C# Connection String for Oracle database is not working 22 51
BULK INSERT most recent CSV 19 21
Square brackets 4 0
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now