Click here to Skip to main content
14,695,235 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I cant able to read selected values in gridview... it returns null value while debugging..
kindly fix this issue..

im trying to send email to selected users frm gridview using checkbox

im getting error in

 string email =row.Cells[2].Text;
                        s.To.Add(email);

It cant the read the selected values in gridview...


source file-------------------
<%@ Page Language="C#" Debug="true" 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    subject
    </td>
    <td>
    <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox>

    </td> </tr>
    <tr>
    <td>message</td>
   <td>
   <asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox>
   </td>
    </tr>
    </table>
    </div>
    <div>

    </div>

    <div>

    </div>
    <div>
    <asp:Button ID="btnsubmit" runat="server" Text="Send" onclick="btnsubmit_Click" />
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="sno">
               <ItemTemplate>
               <asp:Label ID="lbl1" runat="server" Text='<%#bind("sno") %>'></asp:Label>
               </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name">
                <ItemTemplate>
                <asp:Label ID="lbl2" runat="server" Text='<%#bind("name") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="mail">
                <ItemTemplate>
                <asp:Label ID="lbl3" runat="server" Text='<%#bind("mailid") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                <HeaderTemplate>
                                <asp:CheckBox ID="chkhdr" runat="server" />
                            </HeaderTemplate>
                <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>


        </asp:GridView>

    </form>
</body>
</html>


aspx.cs file.........................................


 protected void btnsubmit_Click(object sender, EventArgs e)
    {


        string id = string.Empty;


        MailMessage s = new MailMessage();
        s.From = new MailAddress("priyadharshan91@gmail.com");
        s.Subject = txtsubject.Text;
        s.Body = txtmessage.Text;



        foreach (GridViewRow row in GridView1.Rows)
        {


                CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
                {

                    if (chk.Checked==true)
                    {

                         string email =row.Cells[2].Text;
                        s.To.Add(email);

                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";

                        smtp.EnableSsl = true;

                        NetworkCredential p = new NetworkCredential();
                        smtp.UseDefaultCredentials = false;
 smtp.Credentials = new System.Net.NetworkCredential("priyadharshan91@gmail.com", "********");

                        smtp.EnableSsl = true;

                        smtp.Send(s);
                    }
                }

            }
        }
Posted

Check out the following example and adapt it to your need:
on the aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default14.aspx.cs" Inherits="Default14" %>
<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

on the 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;

public partial class Default14 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            return;
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Student ID");
        dtTemp.Columns.Add("First Name");
        dtTemp.Columns.Add("Last Name");
        dtTemp.Rows.Add("1", "Peter", "Leow");
        dtTemp.Rows.Add("2", "James", "Song");
        dtTemp.Rows.Add("3", "Mick", "Sam");
        dtTemp.Rows.Add("4", "MAry", "Cool");
        GridView1.DataSource = dtTemp;
        GridView1.DataBind(); 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
            if (chkBox != null && chkBox.Checked)
            {
                TextBox1.Text = row.Cells[1].Text.ToString(); 
            }
        }
    }
}
   
Comments
VICK 18-Mar-14 8:13am
   
Straight to the point. My 5+.
Peter Leow 18-Mar-14 8:24am
   
Thank you.
Hai
If u checked check box and if u click button,the code goes inside of if condition or not ?
if (chk.Checked==true)
                   {}


pls use update panel then only checked box not clear when page get post back.R u bind data in gridview inside of IsPostBack in page load ?

Post ur data bind code in gridview form page load event.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900