Monday, 19 November 2012

Ajax AutoCompleteExtender without using web service file in Asp.net in c#.


Hi Friends , In the previous Ajax post I had explained Ajax AutoCompleteExtender control using web service file in ap.net in c#. In this post , I have explained AutoCompleteExtender control without web service file in Asp.net using c#.
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
            </asp:ScriptManager>

<asp:TextBox ID="txt_country" runat="server"></asp:TextBox>
                        <asp:AutoCompleteExtender ID="txt_Student_Class_Section_AutoCompleteExtender"
                                FirstRowSelected="false"     EnableCaching="false"
                                runat="server" TargetControlID="txt_country"  ServiceMethod="search_text"
                                CompletionSetCount="10" CompletionInterval="100" MinimumPrefixLength="1"
                                ShowOnlyCurrentWordInCompletionListItem="True" UseContextKey="True">
                            </asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>

Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }


    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public string[] search_text(string prefixText)
    {
        SqlConnection con = new SqlConnection("Data Source
=SUSHIL-PC; Initial Catalog
=modi ;Integrated Security=true;");
        con.Open();
        SqlCommand com = new SqlCommand("select CountryName from Country where CountryName LIKE '" + prefixText + "%' ", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        string[] str = new string[ds.Tables[0].Rows.Count];
        int i=0;
        try
        {
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                str.SetValue(dr["CountryName "].ToString(),i);
                i++;
            }
        }
        catch{}
        finally
        {
            con.Close();
        }
        return str;


    }

}

If you really like this post , please give a comment because it will helpful who are new  comers in Asp.Net. 

Sunday, 18 November 2012

Fibonacci series without recursion in c#.


Fibonacci Series code in c# without recursion.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(".........Fibonacci Series......");
            int i = 0; int j = 1; int k = 1;
            Console.Write("Enter the no:");
            int m =Convert.ToInt32(Console.ReadLine());
            Console.Write("series are:");
            int n;
            Console.Write(" {0} {1} ", i,j);
            for (n = 0; n <= m;n++ )
            {
                k = i + j;
                i = j;
                j = k;
               
                Console.Write(" {0} ",k);
            }
            Console.ReadKey();
        }
    }
}

Saturday, 17 November 2012

Ajax AutoCompleteExtender Control in Asp.Net in C# using WebServices.


Hi friends , Solution is here.
Here I  have explained AutoCompleteExtender Control using WebServices file.

First Create a web site in visual studio. In this project add a webservices file. Code for Webservices file is given below.

WebServices.asms.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
  
    public string[] search_source_name(string prefixText)
    {
        SqlConnection con = new SqlConnection("server=SUSHIL-PC;database=BusReservation ;Integrated Security=true;");
        con.Open();
        SqlCommand com = new SqlCommand("select CountryName from Country where CountryName LIKE '" + prefixText + "%' ", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        string[] str = new string[ds.Tables[0].Rows.Count];
        int i=0;
        try
        {
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                str.SetValue(dr["CountryName "].ToString(),i);
                i++;
            }
        }
        catch{}
        finally
        {
            con.Close();
        }
        return str;


    }
}

After that come to design page. Design the page as the code give below:-
Default.aspx:-

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
                <asp:ServiceReference Path="~/WebService.asmx" />
            </Services>
        </asp:ScriptManager>
    <ul>
        <asp:Label ID="Label1" runat="server" Text="CountryName"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
            DelimiterCharacters="" MinimumPrefixLength="1" Enabled="True" EnableCaching="true"
            ServiceMethod="search_source_name" ServicePath="~/WebService.asmx"
            TargetControlID="TextBox1" CompletionInterval="100">
        </asp:AutoCompleteExtender>
       
       
    </ul>
    </div>
   
    </form>
</body>
</html>


Run the code see the output how AutoCompleteExtender is working. In Next post of Ajax I will come with AutoCompleteExtender without webservice file.  

Friday, 16 November 2012

How to save image in DataBase and Retrieve from Database in asp.net using C#.


 Hi Friend’s, In this post I have explained how to save image in Database and in Specified Folder  with Specified name  in asp.net using c#.
Step1:-
Open visual studio -> create a new web site.
Step2:-
Create a folder  whose name is App_Code inside the application.  In this folder  create one bal.cs,  one dal.cs and one Utile.cs file inside this. See the figure..

Bal.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for bal
/// </summary>
public class bal
{
    dal d1 = new dal();
      public bal()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    public int insert(string Username, string password, string email_id, string image)
    {
        return d1.insert(Username,password,email_id,image);
    }
}

Dal.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for dal
/// </summary>
public class dal
{
      public dal()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    public int insert(string Username,string password, string email_id,string image)
    {
        SqlConnection con = new SqlConnection(@"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True");
        SqlCommand com = new SqlCommand("sp_insert",con);// here sp_insert is stored procedure
        con.Open();
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@username",Username);
        com.Parameters.AddWithValue("@password",password);
        com.Parameters.AddWithValue("@email",email_id);
        com.Parameters.AddWithValue("@photo",image);
        return com.ExecuteNonQuery();
        con.Close();
    }
}

Utile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Util
/// </summary>
public class Util
{
      public Util()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    public static string IMAGE = "image\\";
    public static string TILED_ROOT = "~/";
    public static string ROOT_PATH = System.Web.HttpContext.Current.Server.MapPath("~/");
    public static string makePictureName( String orignalFileName)
    {
        return getName(orignalFileName) + "." + getExtension(orignalFileName);
    }
    public static string getName(String fileName)
    {
        if (fileName.Contains("."))
        {
            return fileName.Split('.')[0].ToString();
        }
        else
        {
            //throw Exception;
            return fileName;

        }
    }
    public static string getExtension(String fileName)
    {
        if (fileName.Contains("."))
        {
            return fileName.Split('.')[1].ToString();
        }
        else
        {
            //throw Exception;
            return fileName;

        }
    }
    public static string makePhysicalPath(String virtualPath)
    {
        return virtualPath.Replace('\\', '/');
    }
    public static string makeVirtulPath(String virtualPath)
    {
        return virtualPath.Replace('/', '\\');
    }
}

Step3:
Make a folder which name is image(in which image is stored) inside the project(see the above figure.)
Step4:
Create the table  and make a stored procedure .
Sql Query:
Create table register(@username varchar(max),@password varchar(50),@email varchar(50),@photo varchar(max));

Stored Procedure:
CREATE PROCEDURE sp_insert
(
@username varchar(max),
@password varchar(50),
@email varchar(50),
@photo varchar(max)
)
AS
BEGIN
     insert into register values(@username,@password,@email,@photo);
END
GO
Step5:Code for Design page and Code behid design means .cs page.
Default.aspx:
<!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>
    UserName:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    Password:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    Email Id:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
    Photo:<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Image ID="Image1" runat="server" /><br />
        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs:-
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;

public partial class _Default: System.Web.UI.Page
{
    bal b1 = new bal();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
       
        string username = TextBox1.Text;
        string password = TextBox2.Text;
        string email = TextBox3.Text;
        string image = FileUpload1.PostedFile.FileName;
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            String picture_path = Util.IMAGE + Util.makePictureName(image);
            FileUpload1.PostedFile.SaveAs(Util.ROOT_PATH + picture_path);
            Image1.ImageUrl = Util.TILED_ROOT + Util.makePhysicalPath(picture_path);
            b1.insert(username, password, email, picture_path);
        }
       
    }
}

Step6:-  This is final step . Debug the program and run the code. If any problem regarding this post you can contact me at  sushilct86@gmail.com. In next I will come with how show image from DataBase.
Please do not forget to comment for this post. You comment is very important for other whose are reading this blog. 

Thursday, 15 November 2012

Ajax CalenderControl Code in Asp.Net.


Ajax CalenderControl is used to pick up the date from Ajax calendar in web page. It also take a date with time . You can customized as per your requirement.
If you want to show the calendar on your website then you can use ajax calendar control also.
Calender.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxCalenderControl.aspx.cs" Inherits="Ajax_AjaxCalenderControl" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!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>
        <asp:Label ID="Label1" runat="server" Text="Date"></asp:Label>&nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
            Enabled="True" Format="dd-mm-yyyy" TargetControlID="TextBox1">
        </asp:CalendarExtender>
    </div>
    </form>
</body>
</html>