Visual Studio & ASP.NET Database Help

COBOL

Active Member
Joined
Jan 16, 2012
Messages
69
Reaction score
0
Hi Everyone

So I am doing a University Project where I have to create a website using Visual Studio 2010, Microsoft SQL Server and ASP.Net. I am a complete beginner in creating websites in VS2010 and ASP.NET. In a previous course, I created an entire web system using PHP, JavaScript and the database used was MySQL.

This is the problem I am having:
I am trying to read data from a database table, and I need to display that data in an HTML paragraph. In terms of pseudocode, this is what I want to do:

for each record in the database table BLOG:
<h1>#Blog Heading Read In From Database Table#</h1>
<p>#Blog Content Read In From Database Table#</p>
next record

However all the tutorials and videos I have seen tell me that to access the DB from ASP.NET in VB2010, I need to use a SQLDataSource object and using this object I can only display the data in a GridView, DetailsView or FormView - however none of these satisfy my needs.

Could anyone tell me how (or link me to a tutorial) I can achieve what I need to do? I would REALLY appreciate all help!
 
I dont know ASP.net myself, but I am pretty sure SQLDatasource will have an Eof(), and Next() methods.

it will also have something like FieldByName("fieldname).Value();

so, you would do something like this

Code:
dataSource.First();
while not dataSource.Eof() {
   <h1>dataSource.FieldByName("blog_heading").Value();</h1>
   <p>datasource.FieldByName("blog_content").Value();</p>
   dataSource.Next();
}

my syntax is obviously not correct, having never used .net :)
 
You can loop through a dataSource and append to a StringBuilder. _kabal_'s method looks like VB.NET, mine is more like C#.NET

Just follow the tutorials until it wants you to bind the dataSource to the GridView (or whatever) then (pseudocode)

var sb = new StringBuilder();
if(dataSource.Tables.Count > 0)
{
for(var row in dataSource.Tables[0].Rows)
{
sb.append("<h1>" + row["blog_heading"] + "</h1>");
sb.append("<p>" + row["blog_content"] + "</p>");
}
}
 
The way I work with data in .net is like this.

I would pull all the data into a dataset and then call the fields you want from it.

''' <summary>
''' Return dataset
''' </summary>
''' <param name="Query">Query must be assigned in the #Region "SQL Queries" section</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function SQL_GET_DATASET(ByVal Query As String) As DataSet
Dim ds As New DataSet
Try
Using con As New SqlConnection
Using cmd As New SqlCommand
con.ConnectionString = My.Settings.ConString
cmd.Connection = con
cmd.CommandText = Query
con.Open()
Dim SQLAdapter As New System.Data.SqlClient.SqlDataAdapter(cmd.CommandText, con.ConnectionString)
SQLAdapter.Fill(ds)
con.Close()
End Using
End Using
Catch ex As Exception

End Try
Return ds
End Function
 
Top
Sign up to the MyBroadband newsletter
X