1

I think what I need is simple but I can't achieve it through asp.net because I am a total beginner.

What I need is to display a field from sql db table to my webpage like this example:

Account Information 

    Your Name is: <Retrieve it from db>
    Your Email is: <Retrieve it from db>

How should I do that ?

I already have table members.

I need to do this with c# , I am using Visual Studio Web Express 2010

1
  • Your question title says that it is a asp.net webpage, but in your question you state that you are using C#. Could you please clarify which you mean? Commented Dec 11, 2010 at 16:40

1 Answer 1

4

First step is add the SQL Client namespace:

using System.Data.SqlClient;

DB Connection

Then we create a SqlConnection and specifying the connection string.

SqlConnection myConnection = new SqlConnection("user id=username;" + 
                                       "password=password;server=serverurl;" + 
                                       "Trusted_Connection=yes;" + 
                                       "database=database; " + 
                                       "connection timeout=30");

This is the last part of getting connected and is simply executed by the following (remember to make sure your connection has a connection string first):

try
{
    myConnection.Open();
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());
}

SqlCommand

An SqlCommand needs at least two things to operate. A command string, and a connection. There are two ways to specify the connection, both are illustrated below:

SqlCommand  myCommand = new SqlCommand("Command String", myConnection);

// - or -

myCommand.Connection = myConnection;

The connection string can also be specified both ways using the SqlCommand.CommandText property. Now lets look at our first SqlCommand. To keep it simple it will be a simple INSERT command.

SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                     "Values ('string', 1)", myConnection);

// - or - 

    myCommand.CommandText = "INSERT INTO table (Column1, Column2) " + 
                            "Values ('string', 1)";

SqlDataReader

Not only do you need a data reader but you need a SqlCommand. The following code demonstrates how to set up and execute a simple reader:

try
{
    SqlDataReader myReader = null;
    SqlCommand    myCommand = new SqlCommand("select * from table", 
                                             myConnection);
    myReader = myCommand.ExecuteReader();
    while(myReader.Read())
    {
        Console.WriteLine(myReader["Column1"].ToString());
        Console.WriteLine(myReader["Column2"].ToString());
    }
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}
Sign up to request clarification or add additional context in comments.

6 Comments

yes i need to use sql server , and i successfully do an insert but if i want just to post welcome,( user name from db ) , should i do all of this to post only a name !! ?
Yes, you would need to follow these steps. Don't worry, it becomes easy after you perform the initial setup and get it connected to your db.
i understand like 70% but how should i put put column1 value beside " First Name " in my page ?
Use SqlCommand to query for "First Name", use SqlDataReader to read the value and convert it into a string, and finally concatenate that value to your other string text.
ok now for the try {} statement , where should i put that ? , i already put my connection like this: public partial class clients_Default : System.Web.UI.Page { SqlConnection badersql = new SqlConnection("Data Source=BADER-VAIO\\SQLEXPRESS;Initial Catalog=webage;Persist Security Info=True;User ID=sa;Password=123"); , now everytime i try to put the "try { } " statement , its gives me an error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.