Click here to Skip to main content
15,900,973 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Gridview with Checkboxes Pin
Brij5-Dec-08 2:56
mentorBrij5-Dec-08 2:56 
AnswerRe: Gridview with Checkboxes Pin
Nishant Singh5-Dec-08 2:53
Nishant Singh5-Dec-08 2:53 
GeneralRe: Gridview with Checkboxes Pin
premprakashbhati5-Dec-08 3:04
premprakashbhati5-Dec-08 3:04 
GeneralRe: Gridview with Checkboxes Pin
Nishant Singh5-Dec-08 3:13
Nishant Singh5-Dec-08 3:13 
GeneralRe: Gridview with Checkboxes Pin
premprakashbhati5-Dec-08 3:20
premprakashbhati5-Dec-08 3:20 
GeneralRe: Gridview with Checkboxes Pin
Nishant Singh5-Dec-08 3:29
Nishant Singh5-Dec-08 3:29 
GeneralRe: Gridview with Checkboxes Pin
premprakashbhati5-Dec-08 3:47
premprakashbhati5-Dec-08 3:47 
GeneralRe: Gridview with Checkboxes Pin
premprakashbhati9-Dec-08 2:46
premprakashbhati9-Dec-08 2:46 
good evening ,
i am trying new code now....here i can bring all checkbox selected fields PTittle value in a string gvID's...and passing to delete command...

but at ExecuteNonQuery() the Error raised that Incorrect Syntax near gvID's value...(for eg:Political Influence on Students is the value in
the string contains PTittle Values Collection...saying Syntax Error near Influence....

Here is the Code: .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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="PTittle">
            <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                                <input type="checkbox"  id="CheckAll" onclick="return check_uncheck (this );" runat="server" />
                        </HeaderTemplate>
                         <ItemTemplate>
                                <asp:Label ID="tittle" Visible="false" 
                                Text='<%# DataBinder.Eval (Container.DataItem, "PTittle") %>' runat="server" />
                                <input type="checkbox"  id="deleteRec" onclick="return check_uncheck (this );" runat="server" />
                           </ItemTemplate>
                       </asp:TemplateField>
                <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
                <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                <asp:BoundField DataField="PTittle" HeaderText="PTittle" SortExpression="PTittle" />
                <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
                <asp:BoundField DataField="Tags" HeaderText="Tags" SortExpression="Tags" />
                <asp:BoundField DataField="PStatus" HeaderText="PStatus" SortExpression="PStatus" />
                <asp:BoundField DataField="TotalComments" HeaderText="TotalComments" SortExpression="TotalComments" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClientClick="return confirmMsg(this.form)" Text="Button" OnClick="Button1_Click" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BlogDatabaseConnectionString %>"
            SelectCommand="SELECT [Author], [Date], [PTittle], [Category], [Tags], [PStatus], [TotalComments] FROM [Posts]">
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
<script type="text/javascript" language="javascript" >
    function check_uncheck(Val)
    {
         var ValChecked = Val.checked;
         var ValId = Val.id;
         var frm = document.forms[0];
        // Loop through all elements
        for (i = 0; i < frm.length; i++)
        {
        // Look for Header Template's Checkbox
        //As we have not other control other than checkbox we just check following statement
         if (this != null)
            {
                if (ValId.indexOf('CheckAll') !=  - 1)
                    {
                        // Check if main checkbox is checked,
                     // then select or deselect datagrid checkboxes
                        if (ValChecked)
                        {
                         frm.elements[i].checked = true;
                         }
                         else
                         {
                         frm.elements[i].checked = false;
                         }
                   }
        else if (ValId.indexOf('deleteRec') !=  - 1)
        {
             // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
            if (frm.elements[i].checked == false)
                {
                frm.elements[1].checked = false;
                }
      }
    } // if
  } // for
} // functionNow the Delete confirmation method:



function confirmMsg(frm)
{
  // loop through all elements
    for (i = 0; i < frm.length; i++)
    {
        // Look for our checkboxes only
           if (frm.elements[i].name.indexOf("deleteRec") !=  - 1)
            {
                // If any are checked then confirm alert, otherwise nothing happens
                if (frm.elements[i].checked)
                {
                    return confirm("Are you sure you want to delete your selection(s)?")
                }
            }
     }
}

</script>









Here is the Code: .aspx.cs




using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string gvIDs = "";
        bool chkBox = false;
        //'Navigate through each row in the GridView for checkbox items
        foreach (GridViewRow gv in GridView1.Rows)
        {
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)gv.FindControl("deleteRec");
           
            if (deleteChkBxItem.Checked)
            {
                chkBox = true;
                // Concatenate GridView items with comma for SQL Delete
                gvIDs += ((Label)gv.FindControl("tittle")).Text.ToString() + ",";
            }
        }

        SqlConnection cn = new SqlConnection(SqlDataSource1.ConnectionString);
        if (chkBox)
        {
            // Execute SQL Query only if checkboxes are checked to avoid any error with initial null string
            try
            {
                //string deleteSQL = "DELETE from Posts WHERE PTittle IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
                SqlCommand cmd = new SqlCommand("DELETE from Posts WHERE PTittle IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")", cn);
                cn.Open();
                cmd.ExecuteNonQuery();//Error:After ExecuteNonQuery
                GridView1.DataBind();
            }
            catch (SqlException err)
            {
                Response.Write(err.Message.ToString());
            }
            finally
            {
                cn.Close();
            }

        }
    }
}

QuestionRemote loggers in .Net Pin
John.G5-Dec-08 2:23
John.G5-Dec-08 2:23 
AnswerRe: Remote loggers in .Net Pin
SeMartens5-Dec-08 2:35
SeMartens5-Dec-08 2:35 
AnswerRe: Remote loggers in .Net Pin
Abhijit Jana5-Dec-08 3:11
professionalAbhijit Jana5-Dec-08 3:11 
QuestionBuilt-in Functions in .Net Pin
John.G5-Dec-08 2:20
John.G5-Dec-08 2:20 
AnswerRe: Built-in Functions in .Net Pin
SeMartens5-Dec-08 2:38
SeMartens5-Dec-08 2:38 
AnswerRe: Built-in Functions in .Net Pin
Brij5-Dec-08 2:48
mentorBrij5-Dec-08 2:48 
QuestionFile Handling in Js Pin
John.G5-Dec-08 2:19
John.G5-Dec-08 2:19 
AnswerRe: File Handling in Js Pin
SeMartens5-Dec-08 2:34
SeMartens5-Dec-08 2:34 
GeneralRe: File Handling in Js Pin
John.G5-Dec-08 18:03
John.G5-Dec-08 18:03 
GeneralRe: File Handling in Js Pin
SeMartens6-Dec-08 5:05
SeMartens6-Dec-08 5:05 
GeneralRe: File Handling in Js Pin
John.G8-Dec-08 19:34
John.G8-Dec-08 19:34 
QuestionHow to use AL? Pin
John.G5-Dec-08 2:16
John.G5-Dec-08 2:16 
AnswerRe: How to use AL? Pin
SeMartens5-Dec-08 2:39
SeMartens5-Dec-08 2:39 
AnswerRe: How to use AL? Pin
John.G5-Dec-08 18:01
John.G5-Dec-08 18:01 
Question0:12 to 0:24 format Pin
Krazy Programmer5-Dec-08 1:10
Krazy Programmer5-Dec-08 1:10 
AnswerRe: 0:12 to 0:24 format Pin
Ashfield5-Dec-08 1:22
Ashfield5-Dec-08 1:22 
GeneralRe: 0:12 to 0:24 format Pin
Krazy Programmer5-Dec-08 1:31
Krazy Programmer5-Dec-08 1:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.