Check idCOllection values by putting Break Point, I am 90% sure values are not comming properly in that collection. Also check the vaue of strIDs in final method also change
STILL IT GENERATE ERROR.ALL IS PERFECT APPLICATION RUN TOO.BUT LITTLE PROB I CANT FIND.PLZ CHEK IT.
LINE NUM : string strSql = "Delete from D_and_k WHERE ID in (" + strIDs + ")";
error:Incorrect syntax near ')'.
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.Data.SqlClient; using System.Collections.Specialized; using System.Collections.Generic; using System.Text;
public partial class _Default : System.Web.UI.Page { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlCommand cmd = new SqlCommand(); SqlConnection con; string str; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack)
BindGridView(); totalrecords(); } private void totalrecords() { con = new SqlConnection(connStr); //cmd.Connection = con; con.Open(); str = "select count(*) from employee "; SqlCommand cmd = new SqlCommand(str, con); int row = (int)cmd.ExecuteScalar(); Label2.Text = row.ToString(); con.Close(); } private void BindGridView() {
Just put a checkbox inside gridview using GridView , And Create a separate delete button outside grid... And on Delete loop the grid(using for each loop) , and find checkbox control on every gridviewrow and if its checked delete it.
Sukesh MarlaPosted Aug 23, 2012, 12:54 PM
Check answer as correct answer if it helped
vikas kananiPosted Aug 23, 2012, 12:44 PM
Sukesh MarlaPosted Aug 19, 2012, 7:27 AM
vikas kananiPosted Aug 19, 2012, 7:24 AM
Sukesh MarlaPosted Aug 19, 2012, 7:16 AM
and Query for your Table
vikas kananiPosted Aug 19, 2012, 7:15 AM
Sukesh MarlaPosted Aug 19, 2012, 7:11 AM
Also check the vaue of strIDs in final method
also change
To strIDs =IDs.TrimEnd(',');
vikas kananiPosted Aug 19, 2012, 7:07 AM
Sukesh MarlaPosted Aug 19, 2012, 7:03 AM
Sukesh MarlaPosted Aug 19, 2012, 7:02 AM
vikas kananiPosted Aug 19, 2012, 7:02 AM
vikas kananiPosted Aug 19, 2012, 7:00 AM
Sukesh MarlaPosted Aug 19, 2012, 6:54 AM
Do one thing
Copy Ur ASpX Code then code behind code here
vikas kananiPosted Aug 19, 2012, 6:50 AM
Sukesh MarlaPosted Aug 19, 2012, 6:45 AM
vikas kananiPosted Aug 19, 2012, 6:43 AM
Sukesh MarlaPosted Aug 19, 2012, 6:39 AM
http://www.c-sharpcorner.com/Forums/pagenotfound.aspx
vikas kananiPosted Aug 19, 2012, 6:31 AM
Sukesh MarlaPosted Aug 19, 2012, 6:18 AM
vikas kananiPosted Aug 19, 2012, 6:17 AM
Sukesh MarlaPosted Aug 19, 2012, 6:06 AM
try with this.
Hope it helped check this is correct answer if it is.
vikas kananiPosted Aug 19, 2012, 6:03 AM
Sukesh MarlaPosted Aug 19, 2012, 5:33 AM
so do like
string strIDs=null;
if( IDs .indexOf(",")!=-1)
{
strIDs =IDs.Substring(0, IDs.LastIndexOf(","));
}
else
{
strIDs = IDs ;
}
Hope it helped check this is correct answer if it is.
vikas kananiPosted Aug 19, 2012, 5:30 AM
it code on Akkiraju Ivaturi.plz check it n reply me
vikas kananiPosted Aug 19, 2012, 4:32 AM
Akkiraju IvaturiPosted Aug 19, 2012, 1:55 AM
http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html
Satyapriya NayakPosted Aug 18, 2012, 1:22 PM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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.Data.SqlClient;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindGridView();
totalrecords();
}
private void totalrecords()
{
con = new SqlConnection(connStr);
//cmd.Connection = con;
con.Open();
str = "select count(*) from employee ";
SqlCommand cmd = new SqlCommand(str, con);
int row = (int)cmd.ExecuteScalar();
Label2.Text = row.ToString();
con.Close();
}
private void BindGridView()
{
con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "Select * from employee";
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
private void DeleteRecords(StringCollection sc)
{
con = new SqlConnection(connStr);
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "DELETE FROM employee WHERE EmpId";
sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);
}
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked)
{
id = GridView1.Rows[i].Cells[1].Text;
sc.Add(id);
}
}
}
DeleteRecords(sc);
BindGridView();
totalrecords();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Button b = (Button)e.Row.FindControl("ButtonDelete");
b.Attributes.Add("onclick", "return ConfirmOnDelete();");
}
}
}
Thanks
If this post helps you mark it as answer
Sukesh MarlaPosted Aug 18, 2012, 12:53 PM
And on Delete loop the grid(using for each loop) , and find checkbox control on every gridviewrow and if its checked delete it.
Let me knw if anythng required
If it helped check this is correct answer,.