Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

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

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
DotNetChano
Asked:
DotNetChano
  • 7
  • 6
  • 2
  • +1
1 Solution
 
Imran Javed ZiaCommented:
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
 
DotNetChanoAuthor Commented:
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
 
smanoCommented:
Is this is what you looking for?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
smanoCommented:
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
 
smanoCommented:
missed '>' so line 7 in the end would be '</div>'
0
 
DotNetChanoAuthor Commented:
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
 
DotNetChanoAuthor Commented:
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
 
HairbrushCommented:
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
 
DotNetChanoAuthor Commented:
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
 
HairbrushCommented:
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
 
DotNetChanoAuthor Commented:
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
 
smanoCommented:
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
 
smanoCommented:
And it works just fine.
0
 
DotNetChanoAuthor Commented:
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
 
DotNetChanoAuthor Commented:
thank you!
0
 
smanoCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now