Thursday, May 19, 2011

using sql command techniques insert data into database table

we must add using System.Data.SqlClient; namespace. 
now following code you can use.
This is Vary old Techniques. If we use techniques with Asp.net 
there is Pooling required because if there is large amount of users 
use this page than it is difficult to handle connnection.  
Insert into database using sqlcommand 
using System;
using System.Data;
using System.Data.SqlClient;

   class sqlcommandexample
{
      static void Main() 
      {
string CN="server=(local)\\SQLEXPRESS;database=DBTEST;Integrated Security=SSPI"; 
SqlConnection objCn = new SqlConnection(CN);
         SqlCommand objCmd= objCn.CreateCommand();

         try 
         {
objCn.Open();

objCmd.CommandText = "CREATE DATABASE NewDB";
Console.WriteLine(objCmd.CommandText);

objCmd.ExecuteNonQuery();
Console.WriteLine("Now is Database created and database");
objCn.ChangeDatabase("NewDB");

objCmd.CommandText = "CREATE TABLE aspdotnetTable(COL1 integer)";
Console.WriteLine(objCmd.CommandText);
Console.WriteLine("NO Rows Affected is: {0}", objCmd.ExecuteNonQuery());

objCmd.CommandText = "INSERT INTO aspdotnetTable VALUES (100)";
Console.WriteLine(objCmd.CommandText);
Console.WriteLine("NO Rows Affected is: {0}", objCmd.ExecuteNonQuery());
         
         } catch (SqlException ex) {
         
            Console.WriteLine(ex.ToString());
         
         } finally {  
         
            objCn.Close();
            Console.WriteLine("Connection Closed.");
         
         }
      }
   }

Monday, May 9, 2011

Customize Paging in DataList Data Control

 Custom Paging In Datalist Control
CustomPaging.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="dtlEmp" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            DepID
                        </th>
                        <th>
                            Salary
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
  <asp:Label ID="lblID" runat="server" 
Text='<%# Bind("id") %>'></asp:Label>
                    </td>
                    <td>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    </td>
                    <td>
<asp:Label ID="lblDepID" runat="server" Text='<%# Bind("depid") %>'></asp:Label>
                    </td>
                    <td>
<asp:Label ID="lblSalary" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:DataList>
        <table>
            <tr>
                <td>
<a href="Default.aspx#BookMark" id="First" onserverclick="ShowFirstPage"
runat="server">
                        <img src="firstpage.gif" alt="" />
                    </a>
                </td>
                <td>
 <a href="Default.aspx#BookMark" id="Prev" onserverclick="ShowPrevPage"
runat="server">
                        <img src="prevpage.gif" alt="" />
                    </a>
                </td>
                <td>
<a href="Default.aspx#BookMark" id="Next" onserverclick="ShowNextPage"
runat="server">
                        <img src="nextpage.gif" alt="" />
                    </a>
                </td>
                <td>
<a href="Default.aspx#BookMark" id="Last" onserverclick="ShowLastPage"
runat="server">
                        <img src="lastpage.gif" alt="" />
                    </a>
                </td>
            </tr>
        </table>
        <asp:Label ID="lblCurrentIndex" runat="server" Text="0"></asp:Label>
        <asp:Label ID="lblPagesize" runat="server" Text="5"></asp:Label>
        <asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

CustomPaging.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection objcn;
    DataSet objDs;
    SqlDataAdapter objAd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }
    public void BindData()
    {
        string Query="select * from tblEmp";
        objcn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"]
.ConnectionString);
            objAd = new SqlDataAdapter(Query, objcn);
            objDs = new DataSet();
        if (!IsPostBack)
        {
            objAd.Fill(objDs);
            lblRecordCount.Text=(objDs.Tables[0].Rows.Count).ToString();
            //objDs=null;
            //objDs=new DataSet();
       
        }
        objAd.Fill(objDs,Convert.ToInt32(lblCurrentIndex.Text),Convert.ToInt32
(lblPagesize.Text),"tblEmp");
        dtlEmp.DataSource = objDs.Tables["tblEmp"].DefaultView;
        dtlEmp.DataBind();

    }
    public void ShowFirstPage(object sender, EventArgs e)
    {
        lblCurrentIndex.Text = "0";
        BindData();
    }
    public void ShowPrevPage(object sender, EventArgs e)
    { 
        lblCurrentIndex.Text=((Convert.ToInt32(lblCurrentIndex.Text))-Convert.
ToInt32(lblPagesize.Text)).ToString();
        if(Convert.ToInt32(lblCurrentIndex.Text)<0)
        {
            lblCurrentIndex.Text="0";
        }
        BindData();
   
    }
    public void ShowNextPage(object sender, EventArgs e)
    {
        if (Convert.ToInt32(lblCurrentIndex.Text) + 
Convert.ToInt32(lblPagesize.Text) < Convert.ToInt32(lblRecordCount.Text))
        {
            lblCurrentIndex.Text = (Convert.ToInt32(lblCurrentIndex.Text)
+ Convert.ToInt32(lblPagesize.Text)).ToString();

        }
        BindData();
    }
    public void ShowLastPage(object sender, EventArgs e)
    {
        int Mod = Convert.ToInt32(lblRecordCount.Text) /
Convert.ToInt32(lblPagesize.Text);
        if (Mod > 0)
        {
            lblCurrentIndex.Text = (Convert.ToInt32(lblRecordCount.Text) -
Mod).ToString();

        }
        else
        { 
        lblCurrentIndex.Text=(Convert.ToInt32(lblRecordCount.Text)-
Convert.ToInt32(lblPagesize.Text)).ToString();
        }
        BindData();
   
    }

}

Wednesday, April 27, 2011

Property in Tutorial in C sharp

Property is Smart Method.
below given property example.