Friday 1 February 2013

How to Bind Hyperlink control in asp.net


(1)Bind Image Control using SqlDataReader class
Database Table Design diagram:
ImageControl
Database Table Query Design:
ImageQuery
Record view of the Table
Table record

Step-1 : Drag and Drop Hyperlink control from ToolBox to Design window
hyperlink
Set property of the label control
Id= hypercontrol
Step-2: Open .CS file(code behind or logical file)
Add one namespace in your .cs
(a) using System.Data.SqlClient:
According to microsoft msdn network “The.NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space.”

namespace

Step-3 Make connection to the MSSqlServer using SqlConnection Class
//So first we need to create object of the SqlConnection class
SqlConnection con = new SqlConnection();
//Through this object , call ConnectionString property of SqlConnection class
//and assign connectionString which is contain four required parameter such as
//Servername,Databasename,userId,Password
con.ConnectionString = @"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebSite16-20130119213411;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebSite16-20130119213411.mdf";
//here use “@” for single backslash if you want without “@” sign then use double backslash in your connection string
//After assign connection string to Sqlconnection object. Call Open() method of SqlConnection class
con.Open();
//Perform Your DQL(select method) Query using SqlCommand class so first we need to create object of this class
SqlCommand cmd = new SqlCommand();
//Write your Select query using where condition and Assign to CommandText Property of SqlCommand class
cmd.CommandText="select * from [Table_name] where sno=1";
//After that connect your command with Sqlconnection so assign con instance to command object
cmd.Connection=con;
//Take SqlDataReader class for reading Sql data
//Call ExecuteReader() method (only for DQL Queries) for SqlDataReader class
 rd = cmd.ExecuteReader();
//after that use read() method for reading all data and assign to image control
 while (rd.Read())
        {
           hypercontrol.NavigateUrl = rd["Photourl"].ToString()+"<br/>";
 
        }http://www.careersoft-technology.com/how-to-bind-hyperlink-control-in-asp-net/

No comments:

Post a Comment