ASP.NET C# – SQL Server – Display Data From a DB on a web page Part 1 of 2

ASP.NET C# – SQL Server – Display Data From a DB on a web page Part 1 of 2


hello on this video I’m going to show you how to create a very simple web page and then query a database and display the data
from the database on the web page I’m going to be using Visual Studio 2015 and ASP.NET C#
before even talk about code let’s take a look on the database. I have a
database called GrievousDB and just one simple table. So, I am going to right click on my table and scrip table as select new query editor. So, this here is my query and if I run this query I get some data so I just played these data right here
on my webpage. So, I am going to use this query on the web page. So, let’s go to Visual Studio now, I am going to create a new project, empty project, right click on my project, add web
form and then right click and set as start page. So now we’re ready to start
coding. Right here on my index.aspx I am going to to delete the form and, I am going to add an ASP label, and that’s all for our page let’s go to the code behind and let’s write some code. The first thing here is to connect to the database for that, we need the connection string. So my connection history is going to be this. Let’s go over it to understand what this means. So I have here a couple of things. So my server is
the IP address of my server. If you are using your serving the same machine that should be
localhost the database is GrievousDB and that is this database here that I’m connecting to the user IDsand the password is.
Now I create this user on a previous video, if you haven’t seen the previous video yet, go back and watch it.
Now, it’s very very important to notice that these is not
safe at all I’m creating these only for learning purposes if we were going to deployed these to production, I would definitely use what’s called the trusted connection but for learning purpose, this is good enough. So, now let’s create our SQL connectionsNow, if you look close right here, you can see there’s a
red squiggly that means Visual Studio does not understand what sqlconnection we are talking about. Let’s try to fix it show potential fixes none of them is
helping us out. So let me try to fix that case. Yes, so, I want to use the System.Data.SqlClient.
If you don’t want to do that, just come here and add this line here and then
everything is going to be OK for you. So now we’re good here, opss I forgot the new and
the next step is to open the connection soand I always like to close the connection right after I open it I do that because otherwise I will
forget to close the connection later so the next step is to create a command
soand the command that goes here is
the commands that we are using on the SQL Server Management Studio to quey. So, I am going to copy this and I am going to paste it right here. So, next I need to tell what
kind of comment typed that is soI am going to be using text. And the reason I am going to be using text is because right here on line 21, my command is a text also it’s very important to notice that this is is not safe at all, so if you are going to go to production, I would highly recommend you to use something that’s called procedure. It is a loss a lot safer and faster than a simple text command but this is for again learning purpose so this is
good enough and the third think we needs when creating a command is to associate the command with the connection so
to do that so, now we’re good with the
command and the next step is to read from the database. For that, we’re going to create a reader, and it’sso, it’s
so what that does is starts reading from the database, and
we need a while loop now, and while the reader reads, meaning, while you still have data to read from the database, then you do something with the data. So for now, I am going to create a temporary string here and then I’m gonna start read the
values from the database into the temp. So,and right here inside, I need to know what kind of formation I’m getting. So the first
column is going to be my name so I am going to put the name right here, and then the next
column is address I am going to put the address right here and then my other column is going to be ID I am going to put ID right here, and then last I am going to put a break here only to give a new line so every ends of a row from the database, I’m going to put a new line break right here and then all that’s left now is to display this data
on the page soand temp happens to be all the data from the
database. So let’s run this code now and there it is. It’s a very simple web page and all I am doing is reading from the database and displaying for the user. So
let’s go over here what I have in my file, in my index.aspx.cs file, so right here in the using I just have one new line, which is line number three and then connect to the database, create a command, create a temporary string read from the database and save this data on a temp string, close the database, very important, and then
display to the user well thanks for watching this is
very easy and you can do it

Daniel Ostrander

Related Posts

11 thoughts on “ASP.NET C# – SQL Server – Display Data From a DB on a web page Part 1 of 2

  1. An Ed To Vi says:

    Work, excellent thank you!

  2. OmRajesh K Bangalore says:

    Thanks alot for the video….

  3. Jethro Lim says:

    what's the title of the previous video?

  4. Thanbir Tamim says:

    love u bro…thanks a lot

  5. Janardhan Reddy says:

    thank you so much !!!Gen Grievous

  6. Renato says:

    Thank you so much, really helpful !

  7. bilal habib says:

    while connecting to database is it compulsory to give login id and password.. cant we just get access by giving server and database name by still remaining in window authentication mode for sql server

  8. Caryen Cheong says:

    Great video! What if my command is SELECT * FROM [tableName]? What should I put in the while loop?

  9. Mohit Kumar says:

    what is the title of the previous video where you set up the user id and password ? please help out.

  10. GuitarrasSJC says:

    Brazilian accent? Excellent tutorial by the way.

  11. Andrew NL says:

    How do i use a db field for the page field? Label seems to not work for me. You have helped me a great amount as well, so thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *