Saturday, March 24, 2012

Delete Data In GridView Vb.net


Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        cn = New SqlConnection(" Write Connection String ")
        da = New SqlDataAdapter("select * from  s_reg", cn)
        Dim cmd As SqlCommandBuilder = New SqlCommandBuilder(da)
        ds = New DataSet
        da.Fill(ds)
        ds.Tables(0).Rows(e.RowIndex).Delete()
        da.Update(ds)
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub

Saturday, March 3, 2012

Create EXE or Setup File in .NET Windows Application

Setup projects allow you to create installers in order to distribute an application. The resulting Windows Installer (.msi) file contains the application, any dependent files, information about the application such as registry entries, and instructions for installation. When the .msi file is distributed and run on another computer, you can be assured that everything necessary for installation is included; if for any reason the installation fails (for example, the target computer does not have the required operating system version), the installation will be rolled back and the computer returned to its pre-installation state.
The following steps will gives the elegant guidance to create an exe or installer file.
1, Go to file menu > click Add > new project >now “Add New Project” dialog will appear.

2.Select the “Other Project Types” and click “Setup and Deployment” projects,Choose “Setup Project”give name project name in name text box finally click OK.

3.New project appear in solution explorer,for eg., you have give the name “MyEXE” file will be displays with given name.

4.Right click the MyEXE > go View > click “File System”

5.You can see the “File System on TargetMachine”under three folders
Application Folder
User’s Desktop
User’s Program Menu

6.Select Application Folder and right click Add>Project Output>select Primary output

7. Select User’s Desktop richt click on the second window>click create new shortcut>select output file from Application folder>change the file name from primary output name to MyEXE

next >>


same procedure follows the user’s program menu also

8.If you want to change the Manufactures name for exe,just right click the project go to properties

change the properties as per you requirement
9.Finally Build the new project After successfully Build the project myEXE(Setup) will be appear in Application Debug or Release folder(depend upon the properties settings)

EXE or installer will be available on his physical path…

When you want to install the EXE on the client machine,you should be installed .NET Framework on that machine because, Applications and controls written for the .NET Framework v2.0 requires the .NET Framework Redistributable Package version 2.0 to be installed on the computer where the application or control runs.

Wednesday, February 29, 2012

GridView vs DataList vs Repeater vs Formview vs DetailsView - which one to choose

Feature                  
Repeater  
 DataList  
 GridView
Table layout
No
No
Yes
Flow layout
Yes
Yes
No
Column layout
No
Yes
No
Style properties
No
Yes
Yes
Templates
Yes
Yes
Columns/optional
Select/ Edit/Delete
No
Yes
Yes
Sort
No
No
Yes
Paging
No
No
Yes

Tuesday, February 28, 2012

What is the difference between a property a method and an event? Give an example of each

1. Text1.color ------ is a property  
2. Text1.text   ------- is Method     
3. button 1. Click  -- is an event
What is the difference between a property a method and an event? Give an example of each.  
*Property are a type of method that shows state of an Object 
like Caption, backcolour  etc.
*Methods/Functions are for changing Or doing something on 
the Object Like Move,Remove, Add etc.
*Event are Objects which means that something happend for 
which the object must respond to. And for this very reason 
we write method like 
button1_Click( sender Object, event Object)
The event Object(Delegate) is related with the method of 
that responder Object(Button)

Monday, February 20, 2012

Fetch the data from Excel File at GridView

1.use this name space:-Imports System.Globalization
2.Add the Excel file in Solution Explorer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fillgried()
    End Sub
    Function fillgrid()
        Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("Customers1.xlsx") + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'"
        Dim query As String = "SELECT * FROM [customers$]"
        Dim excelDataSet As DataSet = New DataSet()
        excelDataSet.Locale = CultureInfo.InvariantCulture
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(query, strConn)
        da.Fill(excelDataSet)
        GridView1.DataSource = excelDataSet.Tables(0)
        GridView1.DataBind()
    End Function

Friday, February 17, 2012

Image Gallery using ASP.Net Grid View control

****Start Script*****
[head id="Head1" runat="server"]
    [title]GridView Images Example[/title]
[script type="text/javascript"]
    function LoadDiv(url) {
        var img = new Image();
        var bcgDiv = document.getElementById("divBackground");
        var imgDiv = document.getElementById("divImage");
        var imgFull = document.getElementById("imgFull");
        var imgLoader = document.getElementById("imgLoader");
        imgLoader.style.display = "block";
        img.onload = function () {
            imgFull.src = img.src;
            imgFull.style.display = "block";
            imgLoader.style.display = "none";
        };
        img.src = url;
        var width = document.body.clientWidth;
        if (document.body.clientHeight > document.body.scrollHeight) {
            bcgDiv.style.height = document.body.clientHeight + "px";
        }
        else {
            bcgDiv.style.height = document.body.scrollHeight + "px";
        }
       imgDiv.style.left = (width - 650) / 2 + "px";
        imgDiv.style.top = "20px";
        bcgDiv.style.width = "100%";
        bcgDiv.style.display = "block";
        imgDiv.style.display = "block";
        return false;
    }
    function HideDiv() {
        var bcgDiv = document.getElementById("divBackground");
        var imgDiv = document.getElementById("divImage");
        var imgFull = document.getElementById("imgFull");
        if (bcgDiv != null) {
            bcgDiv.style.display = "none";
            imgDiv.style.display = "none";
            imgFull.style.display = "none";
        }
    }
[/script]
****End Script***
***Css Start ***
[style type="text/css"]
     body 
     {
        margin:0;
        padding:0;
        height:100%; 
        overflow-y:auto;  
     }
     .modal
     {
        display: none; 
        position: absolute;
        top: 0px; 
        left: 0px;
        background-color:black;
        z-index:100;
        opacity: 0.8;
        filter: alpha(opacity=60);
        -moz-opacity:0.8;
        min-height: 100%;
     }
     #divImage
     {
        display: none;
        z-index: 1000;
        position: fixed;
        top: 0;
        left: 0;
        background-color:White;
        height: 550px;
        width: 600px;
        padding: 3px;
        border: solid 1px black;
     }
     * html #divImage {position:absolute;}
[/style]
***End Css***
***Start Body ***

[body]
    [form id="form1" runat="server"]
        [div]
            [br /]
[asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  Font-Names="Arial"]
[Columns]
    [asp:TemplateField HeaderText="Preview Image"]
        [ItemTemplate]
            [asp:ImageButton ID="ImageButton1" runat="server"
            ImageUrl='<%# Eval("p_image")%>' Width="50px"
            Height="50px" Style="cursor: pointer"
            OnClientClick = "return LoadDiv(this.src);"/]
        [/ItemTemplate]
    [/asp:TemplateField]
[/Columns]
[/asp:GridView]
        [/div]
        [div id="divBackground" class="modal"]
[/div]
[div id="divImage" class = "info"]
    [table style="height: 100%; width: 100%"]
        [tr]
            [td valign="middle" align="center"]
                [img id="imgLoader" alt=""src="images/loader.gif" /]
                [img id="imgFull" runat="server" alt="" src=""style="display: none;
                height: 500px;width: 590px" /]
            [/td]
        [/tr]
        [tr]
            [td align="center" valign="bottom"]
                [input id="btnClose" type="button" value="close"onclick="HideDiv()"/]
            [/td]
        [/tr]
    [/table]
[/div]

 [/form]
[/body]
[/html]


Wednesday, February 15, 2012

display mouseover effect in GridView rows using only CSS?

ASPX Page:-


style type="text/css">
#GridView1 tr.rowHover:hover
{
background-color: Yellow;
font-family: Arial;
}
/style>;
asp:GridView ID="GridView1" runat="server" EnableViewState="false" RowStyle-CssClass="rowHover" ClientIDMode="Static" />




Tuesday, February 14, 2012

How do we read data from a XML file and display it in a DataGrid ?

using ReadXML method
DataSet MyDataset= new DataSet ();
MyDataset.ReadXml (Server.MapPath ("sample.xml"));
MyDataGrid.DataSource =MyDataset;
MyDataGrid.DataBind();

Tuesday, February 7, 2012

Select Year by DropDownList

protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack))
        {
            for (int i = 1980; i < 2020; i++)
                DropDownList1.Items.Add(i.ToString());
            DropDownList1.Items.Insert(0, "Select Year");
        }
    }

Friday, January 20, 2012

update Content of page by admin throw sqldatabase

Admin page...............
Imports System.Data
Imports System.Data.SqlClient

Partial Class admin_Default
    Inherits System.Web.UI.Page
    Public con As SqlConnection
    Public da As SqlDataAdapter
    Public ds As DataSet
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
  con = New SqlConnection("Data Source=OM;Initial Catalog=effort;Integrated Security=True")
 da = New SqlDataAdapter("update page set p_title='" + TextBox1.Text + "',p_description='" +      TextBox2.Text + "',dec='" + Editor1.Content.ToString + "'", con)
   ds = New DataSet
   da.Fill(ds)
   Catch ex As Exception
   Response.Write(ex.Message)
    End Try
    End Sub
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
client.................................
write namespace....
    Function fill() As String
        Try
            con = New SqlConnection("Data Source=OM;Initial Catalog=effort;Integrated Security=True")
            ds = New DataSet
            da = New SqlDataAdapter("select * from page", con)
            da.Fill(ds)
            Label1.Text = ds.Tables(0).Rows(0).Item("dec")
            Label2.Text = ds.Tables(0).Rows(1).Item("p_title")
            Label3.Text = ds.Tables(0).Rows(2).Item("p_description")
           Catch ex As Exception
            Response.Write(ex.Message)

End Try
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fill()
    End Sub

Friday, January 13, 2012

Update data using Admin Panel

write code at the Admin Page 
Add Namespace and add the XML file by Solution Explorer
*********************
Imports System.Xml
Imports System.Web
**********************
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim strxmlpath As String = Server.MapPath("../database/col.xml")
            Dim doc As New XmlDocument
            doc.Load(strxmlpath)
            Dim node As XmlNode = doc.DocumentElement.FirstChild
            node.InnerText = Editor1.Content.ToString  //add the editor on the page
            doc.Save(strxmlpath)
        Catch ex As Exception
            Response.Write(ex.Message)

        End Try
    End Sub
    Public Function xmlshwo2()
        Try
            Dim strxmlpath1 As String = Server.MapPath("../database/col.xml")
            Dim doc As New XmlDocument
            doc.Load(strxmlpath1)
            Dim elem As XmlNode = doc.DocumentElement.FirstChild
            Editor1.Content = elem.InnerText
            doc.Save(strxmlpath1)
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            xmlshwo2()
        End If
    End Sub
write the code on  client  page

Public Function xmlshow1()
        Try
            Dim strxmlpath As String = Server.MapPath("~/database/col.xml")
            Dim doc As New XmlDocument
            doc.Load(strxmlpath)
            Dim elem As XmlNode = doc.DocumentElement.FirstChild
            Label1.Text = elem.InnerText
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        xmlshow1()
    End Sub

Monday, January 9, 2012

Change the background color in same page

***********************
write this code under head> tag
***********************
script type="text/javascript">
function init()
{
document.body.onclick = function()
{
colorChange();
}
function colorChange()
{
var colors = ["red","green","blue","orange","pink"];
var index = Math.floor(Math.random() * 100) % 5;
document.body.style.background = colors[index];
}
}
window.onload = init;
/script>

Friday, January 6, 2012

Sending Email From Your Gmail Account in C#.net

Use these namespace and add function to your event on which you want to send email.
*****************************************************************
using System.Web
using System.Net
using System.Net.Mail
**********************
public void sendml()
{
try
{
SmtpClient client = new SmtpClient();
MailAddress sendto = new MailAddress("email of other person");
MailAddress from = new MailAddress("youremail");
MailMessage message = new MailMessage(from, sendto);
message.IsBodyHtml = true;
message.Subject = "Test by Sandeep";
string st = "Emp Name:" + TextBox1.Text.ToString() + "
Login Time:" + TextBox2.Text.ToString() + "
Logout Time:" + TextBox3.Text + "
Host Name:" + TextBox4.Text + "
Description:" + TextBox5.Text + "";
message.Body = st;
message.IsBodyHtml = true;
NetworkCredential basicauthantication =new NetworkCredential("emailid@gmail.com", "password");
client.Host = "smtp.gmail.com";
client.UseDefaultCredentials = false;
client.Credentials = basicauthantication;
client.EnableSsl = true;
client.Send(message);
Response.Write("mail has been sent");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

Data insert in Gridview

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
public partial class form : System.Web.UI.Page
{
OleDbCommand cmd;
OleDbConnection cn;
OleDbDataAdapter da;
DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
function();
}

protected void Button2_Click(object sender, EventArgs e)
{
cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Sandeep cmc\\Database21.accdb");
da = new OleDbDataAdapter("insert into userr (name1)values('"+TextBox2.Text+"')", cn);
ds = new DataSet();
da.Fill(ds);
function();

}

public void function()

{
cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Sandeep cmc\\Database21.accdb");
da = new OleDbDataAdapter("select * from userr", cn);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


}
}

Thursday, January 5, 2012

Show date And Time in many format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CurrentDate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
Label3.Text = System.DateTime.Now.ToLongDateString();
Label4.Text = System.DateTime.Now.ToLongTimeString();
Label5.Text = System.DateTime.Now.ToShortDateString();
Label6.Text = System.DateTime.Now.ToShortTimeString();
}
}

Find The Host name

private string GetIP()
{
string strHostName = " " ;
strHostName = System.Net.Dns.GetHostName();
return strHostName;
}
And write the code this secssion
protected void Page_Load(object sender, EventArgs e)
TextBox4.Text =GetIP();

Tuesday, January 3, 2012

Deleting selected rows from gridview using checkbox in vb.net

This program will show how to deleting selected rows from gridview using checkbox in vb.net

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page
    Public temptable As DataTable
    Public ds As DataSet
    Public editrow As DataRow
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        temptable = Session("cart")
        editrow = temptable.NewRow
        editrow("Item_Name") = TextBox1.Text
        temptable.Rows.Add(editrow)
        Session("cart") = temptable
        GridView1.DataSource = temptable
        GridView1.DataBind()
    End Sub
'function for filling datatable
    Public Function makecart()
        Try
            temptable = New DataTable("cart")
            temptable.Columns.Add("id", GetType(Integer))
            temptable.Columns("id").AutoIncrement = True
            temptable.Columns("id").AutoIncrementSeed = 1
            temptable.Columns.Add("Item_Name", GetType(String))

            'put table in session
            Session("cart") = temptable

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            makecart()

        End If
    End Sub

    'delete selected items
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        temptable = Session("cart")
        For i As Integer = 0 To temptable.Rows.Count - 1
            If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True Then
                temptable.Rows(i).Delete()
            Else
                Dim a As Integer = 0
            End If
        Next
        Session("cart") = temptable
        GridView1.DataSource = temptable
        GridView1.DataBind()
    End Sub

    'selecting all at a time this handel check box in header row  
    Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.RowCommand
        If CType(GridView1.HeaderRow.FindControl("CheckBox2"), CheckBox).Checked = True Then
            For i As Integer = 0 To GridView1.Rows.Count - 1
                CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True
            Next
        Else
            For i As Integer = 0 To GridView1.Rows.Count - 1
                CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = False

            Next
        End If
   End Sub
End Class