Friday 27 July 2012

How to add link button in gridview in asp.net

How to add link button in gridview and display result in form view after clicking link button on another page

Designer Code

<asp:GridView ID="GridView2" runat="server" >
            <Columns>
            <asp:TemplateField HeaderText="More Info" SortExpression="member_id">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("member_id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="
LinkButton2" runat="server" OnClick="LinkButton2_Click">Get
                        Details</asp:LinkButton>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code Behind

protected void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "More Info";
                e.Row.Cells[1].Text = "Posting Date";
                e.Row.Cells[2].Text = "Want To";
                e.Row.Cells[3].Text = "City";
                e.Row.Cells[4].Text = "Property Type";
                e.Row.Cells[5].Text = "Area(Sq/ft)";
                e.Row.Cells[6].Text = "Property Rent / Price";
                e.Row.Cells[7].Text = "Member Id";
            }
            e.Row.Cells[7].Visible = false;                  // hiding column in runtime
        }


protected void btnSearch_Click(object sender, EventArgs e)
        {
SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter da = new SqlDataAdapter("select query", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView2.DataSource = ds;
            GridView2.DataBind();
         }

protected void LinkButton2_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = (GridViewRow)((LinkButton)sender).Parent.Parent;
            string memberID = gvr.Cells[7].Text;
            Response.Redirect("property-details.aspx?memberID=" + memberID);
        }

On another page.aspx

Designer Code

<asp:FormView ID="FormView1" runat="server">
    <ItemTemplate>
    <table style="width: 90%">
                                <tr>
                                    <td align="right" colspan="2" style="color: Red ; font-size: small;">
                                        <%# Eval("column1") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" style="color: #FFFFFF; font-size: large;" bgcolor="#0066CC"
                                        colspan="2">
                                        Property Details</td>
                                </tr>
                                <tr>
                                    <td align="left" colspan="2" style="color: #000000;">
                                        <br /><b>Address:</b>&nbsp;&nbsp;<%# Eval("column2") %>
                                        <br /><br />
                                    </td>
                                </tr>
                                                                   </table>
    </ItemTemplate>
    </asp:FormView>

Code Behind

protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SqlConnection con = new SqlConnection(strConnString);
                SqlDataAdapter da = new SqlDataAdapter("select ... where member_id='" + Request.QueryString["memberID"].ToString() + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                FormView1.DataSource = ds;
                FormView1.DataBind();
            }

Saturday 7 July 2012

Image insert and retrieval with database

//Insert Image
protected void btnAdd_Click(object sender, EventArgs e)
        {
            //Condition to check if the file uploaded or not
                if (FileUpload1.HasFile)
                {
                //getting length of uploaded file
                int length = FileUpload1.PostedFile.ContentLength;

                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];

                //store the currently selected file in memeory
                HttpPostedFile img = FileUpload1.PostedFile;

                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);

                //use the web.config to store the connection string
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Product VALUES (@ImageName,@Image,@imgCost)", connection);
                cmd.Parameters.Add("@ImageName", SqlDbType.VarChar, 50).Value = txtImageName.Text;
                cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
                cmd.Parameters.Add("@imgCost", SqlDbType.NVarChar, 50).Value = txtImageCost.Text;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                txtImageName.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txtImageName.Text + " image inserted successfully')", true);
                Response.Redirect("~/AdminPanel/Product.aspx");
                }
                }
                }

//Retrive Image

1) Designer Code for dispaly Image in grid view

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" Width="460px" AllowPaging="True"
        PageSize="1" CellPadding="4" ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="ImageId" HeaderText="Image ID" InsertVisible="False"
                ReadOnly="True" SortExpression="ImageId" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="ImageName" HeaderText="Image Name"
                SortExpression="ImageName" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="imgCost" HeaderText="Image Cost"
                SortExpression="imgCost" ItemStyle-HorizontalAlign="Center"/>
                <asp:TemplateField HeaderText="Product Image">
                <ItemTemplate>
                    <asp:Image ID="Image2" runat="server" ImageUrl='<%# "~/ImageHandler.ashx?ImID="+ Eval("ImageId") %>'
                        Height="150px" Width="150px" ImageAlign="Right"/>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></HeaderStyle>
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>


 

Send email with attachment in asp.net


Send email with attachments to any domain email id

Step 1: email.aspx

<table>
<tr>
                            <td align="right" style="width: 98px; color: #000000;">
                                To</td>
            <td>
                <asp:TextBox ID="TextBoxToEmailID" runat="server" Width="385px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Subject</td>
            <td>
                <asp:TextBox ID="TextBoxSubject" runat="server" Width="452px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Attachment</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" style="width: 98px; color: #000000;">
                Email</td>
            <td>
                <asp:TextBox ID="TextBoxEmailBody" runat="server" Height="175px"
                    TextMode="MultiLine" Width="461px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td align="left">
                <asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="right" style="width: 98px">
                &nbsp;</td>
            <td align="left">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnSend" runat="server" onclick="btnSend_Click" Text="Send"
                    Width="66px" />
                &nbsp;</td>
        </tr>
    </table>

Step 2 email.aspx,cs

using System;
using System.Collections;
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.Net;
using System.Net.Mail;

namespace email
{
    public partial class _Default : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
            TextBoxFromEmailID.Visible = false;
            TextBoxFromEmailID.Text = "Type email id here from which email will be sent";
            TextBoxPassword.Visible = false;
            TextBoxPassword.Text = "Type password of above email id";
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(TextBoxToEmailID.Text);
            msg.From = new MailAddress(TextBoxFromEmailID.Text, "Type sender name here..");
            msg.Subject = TextBoxSubject.Text;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = TextBoxEmailBody.Text;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            if (FileUpload1.HasFile)
            {
                msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
            }
            msg.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(TextBoxFromEmailID.Text, TextBoxPassword.Text);
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            object userState = msg;
            try
            {
                client.Send(msg);
                Label1.Visible = true;
                Label1.Text = "Email sent successfully";
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Label1.Visible = true;
                Label1.Text = ex.Message;
            }
        }
    }
}