csharp

How to Bind RadDropDownList in Telerik using C#.

In this article, we will learn How to Bind RadDropDownList in Telerik using C#. Here I have to show complete Demo of How to Bind RadDropDownList in Telerik using C#.
Screen Shot Describe the output after running the project.

1.Complete Demo of How to Bind RadDropDownList in Telerik using C#.
Go to=>Visual Studio=>Click on File–>New–>Project. looks like screenshot

2.After that Select Web–>Select ASP.net Empty Web Application–>Write the name of the project in Name Field. You Can Change the Name of the project and browse your project at a different location as you wish. After that press ok.


3.After that add one “Web Form” Right click on project BindRadDropDown–>Select Add–>New Item
–>Select Web Form and click on ADD button.
important PointAdd Telerik.Web.Design dll and Telerik.Web.UI dll in “Reference folder”. click this link to download telerikdll
like screenshot.

3. DataBase
Create table tblCountry

Create table tblCountry(
Countryid int primary key identity(1,1),
CountryName varchar(30)
)

4.Insert data into tblCountry Table

/*Insert data into CountryTable*/
insert into tblCountry(CountryName) values('India'),('America'),('Sitzerland'),('Nepal')

5. Create stored procedure to getting the data from a country table “usp_country_get” is procedure Name.

create proc usp_country_get
as
begin
Select * from tblCountry
end

6.Open web.config file and establish a connection with a database.

<connectionStrings>
 <add name="DBCS" connectionString="data source=.; initial catalog=DropDownListEx;integrated security=true"/>
 </connectionStrings>


Note
In initial catalog write Database Name
In data source you can simply write (.) /your server name/(local)
Integrated security for Authentication

7. Open Weborm1.aspx page and write this Code
Important Point:
telerik:RadScriptManager:The ScriptManager must appear before any controls that need it.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BindRadDropDown.WebForm1" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="radscriptmanager" runat="server" ></telerik:RadScriptManager>
    <div>
    <telerik:RadDropDownList ID="ddlcountry"  runat="server"></telerik:RadDropDownList>
    </div>
    </form>
</body>
</html>

8. Code for How to Bind RadDropDownList in Telerik using C#.
Open WebForm1.aspx.cs page.Simply write the code here for Bind RadDropDownList in Telerik using C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace BindRadDropDown
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        /// <summary>
        /// Here i have create a sqlconnection object to interact with database
        /// Here "DBCS" taking from Web.config page
        /// </summary>
 
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind_Country();
            }
        }
        /// <summary>
        /// Here i have create method "Bind_Country()" for bind Country
        /// ddlcountry is a name of RadDropDownlist.
        /// its neccesary property its used when you use this control 
        /// in your form.
        /// "usp_country_get" is a procedure name
        /// for Populating the Country 
        /// </summary>
        public void Bind_Country()
        {
            //open the SqlConnection
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_country_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ////close the SqlConnection
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlcountry.DataValueField = "Countryid";
                ddlcountry.DataTextField = "CountryName";
                ddlcountry.DataSource = ds;
                ddlcountry.DataBind();
               
            }
        }

    }
}

Youtube Link

Leave a Reply

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