Friday, January 27

Add a column to a sql server table with a asp.net form

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<div>
<br /><br />
    <asp:Button ID="IP_TextBtn" OnClick="btnAddColumn_Click" runat="server" Text="Submit" />
    <br />
    <br />
    <asp:TextBox id="txtIP_TextField" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label id="lblResults" runat="server" Width="575px" Height="121px" Font-Bold="True"></asp:Label>
    <br />
    <br />
</div>
</form>
</body>
</html>

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

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAddColumn_Click(object sender, EventArgs e)
    {

        {
            string alterSQL;
            alterSQL = "ALTER TABLE TestTable ";
            alterSQL += "ADD " + txtIP_TextField.Text + " BIT";
           // alterSQL = String.Format("ALTER TABLE TestTable  ADD {0} BIT",
                                //txtIP_TextField.Text);
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString());
            SqlCommand cmd = new SqlCommand(alterSQL, con);
            cmd.Parameters.AddWithValue("@txtIP_TextField ", txtIP_TextField.Text);

            int SQLdone = 0;
            try
            {
                con.Open();
                SQLdone = cmd.ExecuteNonQuery();
                lblResults.Text = "Column created.";
            }
            catch (Exception err)
            {
                lblResults.Text = "Error Creating column. ";
                lblResults.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
        }
    }
}

Monday, January 16

JQuery Bind images to dropdownlist in asp.net

Introduction:


In this article I will explain how to bind images to dropdownlist in asp.net using JQuery.
Description:
  
In previous articles I explained about how to implement cascading dropdownlist  and Ajax Cascading dropdownlist. Now I will explain how to bind dropdownlist with images dynamically in asp.net using JQuery. In one of website I saw one dropdownlist that contains Country names along with country flags. I feel it’s better to write post to explain how to bind images to dropdownlist in asp.net. To implement this concept by using JQuery plugin we can achieve this easily.  
To implement concept first design table in your database and enter data as shown below

Column Name
Data Type
Allow Nulls
ID
int(set identity property=true)
No
CountryName
varchar(50)
Yes
CountryImage
varchar(50)
Yes

After completion of table creation enter data in table like as show in below.


Here I am getting images data from already inserted data if you want to dynamically insert images in database check this post insert images in folder and images path in database.

Now open Visual Studio and create new website after that right click on your website and add new folder and give name as Images and insert country flag image in that folder you should get it from attached folder.After that write the following code in your aspx page  


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dropdownlist with Images</title>
<link rel="stylesheet" type="text/css" href="msdropdown/dd.css" />
<script type="text/javascript" src="msdropdown/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="msdropdown/js/jquery.dd.js"></script>
<!-- Script is used to call the JQuery for dropdown -->
<script type="text/javascript" language="javascript">
$(document).ready(function(e) {
try {
$("#ddlCountry").msDropDown();
} catch (e) {
alert(e.message);
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td align="right">
<b>Country:</b>
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" Width="150px" onselectedindexchanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<b>Seelcted Country:</b>
</td>
<td>
<asp:Label ID="lbltext" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
If you observe above code in header section I added some of script files and css file by using those files we have a chance to add images in dropdownlist. To get those files download attached sample code and use it in your application.

After completion of aspx page design add following namespaces in code behind

C#.NET Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After add namespace write the following code


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList();
BindTitles();
lbltext.Text = ddlCountry.Items[0].Text;
}
}
/// <summary>
/// This Method is used to bind titles to each element of dropdown
/// </summary>
protected void BindTitles()
{
if (ddlCountry != null)
{
foreach (ListItem li in ddlCountry.Items)
{
li.Attributes["title"] = "Images/" + li.Value; // setting text value of item as tooltip
}
}
}
/// <summary>
/// Bind Dropdownlist Data
/// </summary>
protected void BindDropDownList()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("select * from CountryImages", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryImage";
ddlCountry.DataSource = ds;
ddlCountry.DataBind();
con.Close();
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
lbltext.Text = ddlCountry.SelectedItem.Text;
BindTitles();
}


VB.NET Code

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Class VbSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDropDownList()
BindTitles()
lbltext.Text = ddlCountry.Items(0).Text
End If
End Sub
''' <summary>
''' This Method is used to bind titles to each element of dropdown
''' </summary>
Protected Sub BindTitles()
If ddlCountry IsNot Nothing Then
For Each li As ListItem In ddlCountry.Items
' setting text value of item as tooltip
li.Attributes("title") = "Images/" & li.Value
Next
End If
End Sub

''' <summary>
''' Bind Dropdownlist Data
''' </summary>
Protected Sub BindDropDownList()
Dim con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
con.Open()
Dim cmd As New SqlCommand("select * from CountryImages", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
ddlCountry.DataTextField = "CountryName"
ddlCountry.DataValueField = "CountryImage"
ddlCountry.DataSource = ds
ddlCountry.DataBind()
con.Close()
End Sub
Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
lbltext.Text = ddlCountry.SelectedItem.Text
BindTitles()
End Sub
End Class
If you observe above code I am binding title to dropdownlist elements by using BindTitles() method. Here our JQuery plugin will take the title of dropdownlist elements and display the images beside of the text.
  
Now run your application and check how your dropdownlist will be 

Demo


Download sample code attached

Saturday, January 7

Source Code of the Free ASP.NET QuickTimePlayer Control

Playing QuickTime movies from an ASP.NET Web form can be a little tricky. At first it may not seem difficult, but there are a lot of little details to worry about — such as browser differences, ActiveX activation, and the list of acceptable parameters. The QuickTimePlayer control detailed here (and shown in Figure 1) takes care of all those issues, reducing the task to drag-and-drop simplicity.


Figure 1: The QuickTimePlayer control eliminates the chores involved with playing QuickTime movies.

How to Use It

The QuickTimePlayer.dll can be added to your Visual Studio toolbox via right-click just like any other control (see end of article for download details). When that task is done, it can be dragged from the toolbox onto any ASP.NET Web form, where a definition similar to this will be rendered to the ASPX:
<cc2:QTPlayer
    ID="QTPlayer1"
    runat="server"
    MOVFile="Sample.mov"
    Width="250" Height="270"
    AutoPlay="true"
    ShowMenu="false">
</cc2:QTPlayer>

The source code for this article is available for download here.
 If you dont have QuickTimePlayer control you can download it from following url:

https://swdlp.apple.com/cgi-bin/WebObjects/SoftwareDownloadApp.woa/wa/getProductData?localang=en_us&grp_code=quicktime&returnURL=http://www.apple.com/quicktime/download 

Thanks
    Shibashish mohanty  

Media Player Contol using Silverlight and ASP.Net

In this article I am explaining the SilverLight Media Player Control in ASP.Net. With the media control one will be able to play stream media like audio, video files over the web page.
To use the SilverLight Media Player Control in ASP.Net. You will have to first download and install
Once all these have installed, you can start building your Media Player Application. You will have to drag the MediaPlayer component from the Toolbox as shown in figure below.


Silverlight MediaPlayer in the Visual Studion 2008 toolbox


By default there are no skins available when you install the .SilverLight Tools you can get the skin files in the Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Server\MediaPlayerSkin Folder. Just add the skins to your project using Add Existing Items in Visual Studio.
Next using the smart tag to choose the skin and add the Media File to be played


Smart Tag to edit properties of SilverLight Media Player


You can also set other parameters like the
Volume - sets the volume of the Media Player Control
Auto-Play – determines whether the media file will be auto played or not when it is loaded.
You can also set the Media Source from code behind as shown below
C#
protected void Page_Load(object sender, EventArgs e)
{
    MediaPlayer1.MediaSource = "~/files/Butterfly.wmv";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MediaPlayer1.MediaSource = "~/files/Butterfly.wmv"
End Sub
Once all these settings have been done you run the application. The figure below displays the SilverLight Media Player.


SilverLight MediaPlayer playing a Windows Media Video (WMV) File


With this we finish the article. You can download the sample source code using the link below.

Thanks
Shibashish mohanty 

Friday, January 6

Downloading Attachment using Response.AddHeader("content-disposition", attachment)

first create a excel file in your local system as well as in your project then take a download link button and write following code
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string fileName = "test.xlsx";
        string filePath = Server.MapPath("~/test.xlsx");
        Response.Clear();

        Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath);
        Response.Flush();
        Response.End();

    }

It will display like










Thanks
shibashish mohanty



.

ShibashishMnty
shibashish mohanty