using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class AccessingData2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Define database connection
SqlConnection conn = new SqlConnection(
"Server=localhost\\\\SqlExpress;Database=Dorknozzle;" +
"Integrated Security=True");
// Create command
SqlCommand comm = new SqlCommand(
"SELECT ID, Title FROM Entries", conn);
// Open connection
conn.Open();
// Execute the command
SqlDataReader reader = comm.ExecuteReader();
// Read and display the data
while (reader.Read())
{
entriesLabel.Text += "<a href='BlogEntry.aspx?ID=" + reader["ID"] + "'>" + reader["Title"] + "</a> | " + "<a href='EditEntry.aspx?ID=" + reader["ID"] + "'>Edit</a>" + " - <a href='DeleteBlogEntry.aspx?ID=" + reader["ID"] + "'>Delete</a>" + "<br />";
}
// TODO: Do something with the data
// Close the reader and the connection
reader.Close();
conn.Close();
addBlogLabel.Text = "<p><a href='AddEntry.aspx'>Would you like to Add a Blog Entry?</a></p>";
}
}
My only concern is this area:
// Define database connection
SqlConnection conn = new SqlConnection(
"Server=localhost\\\\SqlExpress;Database=Dorknozzle;" +
"Integrated Security=True");
// Create command
SqlCommand comm = new SqlCommand(
"SELECT ID, Title FROM Entries", conn);
How do I “drop” the connection string from my CodeBehind file and use my web.config connection data to retrieve Entries results?
Many thanks for all your help, NightStalker. It means a lot.
If you have a chance… you should look into LINQ TO SQL or Entity Framework. Theses are ORM tools which is the new technologies for making sql database and doing data access.
1- What kind of application you doing ? Web Application or Website ?
2- Can you post your header of the file where the errors occurs (I mean the librairies you import ‘using’)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Define database connection
SqlConnection conn = new SqlConnection(
"Server=localhost\\\\SqlExpress;Database=Dorknozzle;" +
"Integrated Security=True");
//SqlConnection conn As String = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
//SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DorkNozzle"]);
// Create command
SqlCommand comm = new SqlCommand(
"SELECT ID, Title FROM Entries", conn);
// Open connection
conn.Open();
// Execute the command
SqlDataReader reader = comm.ExecuteReader();
// Read and display the data
while (reader.Read())
{
entriesLabel.Text += "<a href='BlogEntry.aspx?ID=" + reader["ID"] + "'>" + reader["Title"] + "</a> | " + "<a href='EditEntry.aspx?ID=" + reader["ID"] + "'>Edit</a>" + " - <a href='DeleteBlogEntry.aspx?ID=" + reader["ID"] + "'>Delete</a>" + "<br />";
}
// TODO: Do something with the data
// Close the reader and the connection
reader.Close();
conn.Close();
addBlogLabel.Text = "<p><a href='AddEntry.aspx'>Would you like to Add a Blog Entry?</a></p>";
}
}
It works fine just now, but I get an error as soon as I try to do:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DorkNozzle"]);
Is there anything obvious from the above that I should change to perhaps improve my coding style, without going down the MVC line just yet?
MVC or WebForms or even php it doesn’t matter: directly accessing your storage (might be a database or might be not…) should not be done from page behind, but from a data access layer or a repository .
Almost all the code from Page_Load should belong there. It is very important in the long run to NOT mix the presentation layer with the model layer(mixing tier responsibilities is bad practice, acronyms don’t matter here). For a trivial application you can write all the spaghetti you want, but it will bite you hard in a complex application. And don’t think that you will change your style on the spot. Once you have the “experience” to write spaghetti , you will write it even when you don’t want to, because your mind is trained to.
It 's very important to learn from the start how to write proper code and how to think about your application architecture. Good development practices are hard to master and it’s even harder if you have to change a mindset for that.
I think that asp. net mvc is a much better way to learn how to do it right, instead of WebForms. Be sure to check Asp.Net MVC tutorials and to take it slowly.