Thursday 11 August 2016

Bind Data into Dropdownlist in asp.net using C#


In this Article I'll show you how to bind Dropdown in asp.net C#. In this tutorial I'll show you how data get bind to dropdown list in asp.net using C#.


INITIAL CHAMBER:

Step1)  Open VS10, Make empty Website and give name as "DropDownList".
Step2)  You will see your empty website in Solution Explorer that may be in the right hand side of VS10. Right Click on that website and select “Add New Item". A new Window will open, from that you have to select "web form" and give name as "Dropdownlist.aspx"

In asp.net we use .aspx file for designing purpose, we also make a short design for our project but in later time. You will see solution explorer has made two files "dropdownlist.aspx" and "dropdownlist.aspx.cs". For Just now take it as ".aspx page" is used for "DESIGN PURPOSE" (front end - for advance people) and ".cs page" is used for "CODING PURPOSE"(back end- for advance people).

Step3)  Again go to --> Add New Item and select --> SQL Server Database --> Press OK, they will prompt you by asking "You want to add you database into "App_Data Folder" or not ?" Better you do "YES", the reason behind yes is, If you had chosen "No" than your database will be out of security and it can easily be hacked by SQL Injection attacks but suppose if your database is inside some sort of Security like VS10 have in the form of folder "APP_DATA" than it will be very hard for attackers to get to your database.


Step4)  Open your Server Explorer(If anyhow vs10 is not showing you Server explorer than by going to view-->Server explorer, will enable your server) make it simple by pressing "CTRL+ALT+S" --- HERE YOU GO -- CAN YOU SEE IT ?? TELL ME IN COMMENTS. I'LL BE WAITING..




DATABASE CHAMBER:

Step5)  In Server Explorer, You will see your Database ("Database.mdf") file that you had just added. Click on your Database, you will see a lot of option(Tables,Storeprocedures and many more) For Just now Add Table to your database by going to table --> Right Click and --> Add new table.
Make it like this :




DESIGN CHAMBER:

Step6)  Open your Dropdownlist.aspx page for designing your application. Try to make it like this:
Here is your code to add dropdown from toolbar:

Here is your Design:






CODING CHAMBER:

Step 7) Time to get into the Code. Open your” Dropdownlist.aspx.cs “page, and add some Namespaces:
Here is the Code for binding dropdown:

 protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
           SqlCommand cmd = new SqlCommand("select * from tbl_media", con);
           SqlDataAdapter sda = new SqlDataAdapter(cmd);
          
           DataTable dt = new DataTable();
           sda.Fill(dt);
           DropDownList1.DataSource = dt;
           DropDownList1.DataBind();
          
       }
       

    
   }


You can get your Connection String by going to you database(in server explorer) right Click Properties you can see there “Connection String”. Copy it and paste it in the Sql connection field. Yeah! Surely your connection string is quite different from me, Initially it look like this:




Before:

Connection String  ("Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\Nilesh\Documents\Visual Studio 2010\WebSites\WebSite13\App_Data\Database.mdf";Integrated Security=True;User Instance=True");


You have to remove the path and make it short like:

C:\Users\Nilesh\Documents\Visual Studio 2010\WebSites\WebSite13\App_Data -- > Remove this

And add instead of this |DataDirectory|


After:

Modified Connection String (@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");




OUTPUT CHAMBER:



THANK YOU FOR READING! I HOPE YOU LIKE IT. I AM TRYING TO PULL UP MORE ARTICLES SOONER. STAY TUNED TO COMMENTS AND LIKES HAPPY DAY!




No comments:

Post a Comment