Saturday, May 26, 2012

Get Images from folder and generate HTML anchor in asp.net

Here i will show how to get the image from folder then create a anchor link to that image and assigned to string builder "sb".."sPhysicalPath" is the path of gallery folder.The class Directory Info is used to get the all the image files from the folder
public static string BindImages(int images, bool isRedirecting)
{
string thumbNailFolderName = ConfigurationManager.AppSettings["ThumbFolder"];
string previewFolderName = ConfigurationManager.AppSettings["PreviewFolder"];
StringBuilder sb = new StringBuilder("");
string format = "<span><a href=\"{0}\" title=\"\"><img alt=\"\" src=\"{1}\" /></a></span>";string sPhysicalPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GalleryFolder"]);
DirectoryInfo oDir = new DirectoryInfo(sPhysicalPath);
FileInfo[] oFiles = oDir.GetFiles();
int i = 0;
foreach (FileInfo oFile in oFiles)
{
i++;
sb.AppendFormat(format, (isRedirecting ? "Gallery.aspx" : "school/gallery/" + previewFolderName + "/" + oFile.Name),
"school/gallery/" + thumbNailFolderName + "/" + oFile.Name);
if (i == images && images != 0)
break;
}
sb.Append("");
return sb.ToString();
}

Friday, May 25, 2012

BInd data to dropdownlist in gridview in asp.net

Title: How to bind data to drop down list in asp.net using c#.net

Description:
In previous posts i explained Asp.net bind data to grid view,bind data to dropdownlist,Bind drop down in MVC4.Bind grid view using LINQ. Here i will show how to binding the data to Drop down List inside of Grid View.Here the dropdownlist has place inside of grid view template Field .You can observe here i will bind data to both drop down and grid view While loading page.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Bind data to Dropdownlist in gridview in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvSalesData" AllowSorting="True" Runat="server"
AllowPaging="true" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="SalesID">
 <ItemTemplate>
 <%#Eval("SID")  %>
 </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SalesPName">
 <ItemTemplate>
<%#Eval("SalesPersonName")  %>
</ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField HeaderText="Samount">
 <ItemTemplate>
<%#Eval("Samt")  %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SalesCity">
<ItemTemplate>
<asp:DropDownList ID="ddl_city" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
 </form>
</body>
</html>
Code behind:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Dropdowningridview : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["bhaskarconnection"].Tostring());

protected void Page_Load(object sender, EventArgs e)
{
If(!page.IsPostBack)
{
//Bind data to gridview
BindOrdrGrid();
//Bind data to Dropdown in gridview
BindDropdownGrid();
}
private void BindOrdrGrid()
{
 SqlDataAdapter Ordr_ad = new SqlDataAdapter("select * from Order_details", cn);
 DataSet Ordr_ds = new DataSet();
 Ordr_adp.Fill(Ordr_ds, "Order_details");
 GvSalesData.DataSource = Ordr_ds;
 GvSalesData.DataBind(); 
}
private void BindDropdownGrid()
{
cn.Open();
SqlDataAdapter Ddl_ad = new SqlDataAdapter("select * from order_city", cn);
DataSet Ddl_ds = new DataSet();
ad.Fill(Ddl_ds, "order_city");
foreach (GridViewRow row in GvSalesData.Rows)
{
DropDownList _ddlcity = (DropDownList)(GvSalesData.Rows[row.RowIndex].Cells[4].FindControl("ddl_city"));
_ddlcity.DataSource = Ddl_ds;
_ddlcity.DataValueField = "CID";
_ddlcity.DataTextField = "City_name";
_ddlcity.DataBind();
}
}

Thursday, May 24, 2012

How to Convert SQL Table Data To XML Format in sqlserver

Here i will show how to get the xml output from sql data table.For this we can use FOR XML AUTO statement.The AUTO is one of mode of FOR XML.This sql statement get the emp table data ,then convert in to xml.This can be see in the black rectangle in below screen shot.
Query: select*from emp FOR XML auto

Wednesday, May 23, 2012

How to get random number and string in c#.net

Here i will show how to get the random  text from given user input in asp.net.The method GetRandomTextKey() has taken the length as parameter.The logic"minValue + rndValue * randomRange" has used to generate a random text.Asp.net provide a class which Rnadom is used in this process
 public static string GetRandomTextKey(int length)
    {
        Random rnd = new Random((int)System.DateTime.Now.Ticks);
        System.Text.StringBuilder randomText = new
        System.Text.StringBuilder(length);

        int minValue = 65;
        int maxValue = 90;
        int randomRange = maxValue - minValue;

        double rndValue;
        for (int i = 0; i < length; i++)
        {
            rndValue = rnd.NextDouble();
            randomText.Append((char)(minValue + rndValue * randomRange));
        }

        return randomText.ToString();
    }
              

Jquery keypress,onclick event in sharepoint 2010

Here i will show how to how to use the j query events like key press and on click events in share point.The below peace of code i have used to get the event.i just used two functions one is for key press events and other one is for click event.The main thing here is we should add the j query library to share point application
script><script type="text/javascript">
$('#ctl00_inputTextField').keypress(function() {
getdata(); 
});
function getdata()
{
alert("key press event");
}
//onclick

$(function(){
function clickfunction(event) {
alert('click event');
}
$('#ctl00_inputBtnField').click(clickfunction);
});
</script>

Bel