i want to have checkboxes in my gridview ...by checking it ,i can delete the row/rows.... so i have written code and got 2 Errors ...can anyone solve it...
Error:1...'gridview.RetrieveItems()': not all code paths return a value ,and
Error:2...No overload for method 'DeleteRows' takes '1' arguments Here is my aspx.cs Code: - using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Collections;
-
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.Text;
-
using System.Data.SqlClient;
-
-
public partial class gridview : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
// Include the JavaScript functions during Form_Load() itself.
-
CheckBoxJSFunctions();
-
-
if (!Page.IsPostBack)
-
{
-
PopulateGrid();
-
}
-
}
-
private static DataTable RetrieveItems()<------ //ErrorNo:1
-
{
-
-
//Create a connection to the SQL Server; modify the connection string for your environment
-
//SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Trusted_Connection=yes");
-
SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;UID=myUser;PWD=myPassword;");
-
-
// Create a Command object, and then set the connection.
-
// The following SQL statements check whether a GetAuthorsByLastName
-
// stored procedure already exists.
-
SqlCommand MyCommand = new SqlCommand("select * from sysobjects where id = object_id(N'GetPosts')" +
-
" and OBJECTPROPERTY(id, N'IsProcedure') = 1", MyConnection);
-
-
// Set the command type that you will run.
-
MyCommand.CommandType = CommandType.Text;
-
-
// Open the connection.
-
MyCommand.Connection.Open();
-
-
// Run the SQL statement, and then get the returned rows to the DataReader.
-
SqlDataReader MyDataReader = MyCommand.ExecuteReader();
-
-
// If any rows are returned, the stored procedure that you are trying
-
// to create already exists. Therefore, try to create the stored procedure
-
// only if it does not exist.
-
if(!MyDataReader.Read())
-
{
-
MyCommand.CommandText = "create procedure GetPosts" +
-
" (@date varchar(40),@title varchar(50),(@author varchar(40),@categories varchar(50),(@tags varchar(40),@comments varchar(50)(@status varchar(40), select * from Posts where" +
-
" Date like @date,PTittle like @title,Author like @author,Category like @categories,Tags like @tags,TotalComments like comments,PStatus like @status; select @RowCount=@@ROWCOUNT";
-
MyDataReader.Close();
-
MyCommand.ExecuteNonQuery();
-
}
-
else
-
{
-
MyDataReader.Close();
-
}
-
-
MyCommand.Dispose(); //Dispose of the Command object.
-
MyConnection.Close(); //Close the connection.
-
}
-
-
-
-
-
-
//Call the stored procedure in the Click event of the btnGetAuthors button,
-
-
//DataTable Posts = new DataTable();
-
//DataTable dt = new DataTable();
-
//dt.Load(dr);
-
//if (dt != null)
-
//{
-
// Posts = dt;
-
//}
-
//return dt;
-
-
private void PopulateGrid()
-
{
-
// RetrieveItems() is a user defined function that calls the stored proc to retrieve data from the database.
-
DataTable dtItems = RetrieveItems();
-
GridView1.DataSource = dtItems;
-
GridView1.DataBind();
-
}
-
-
private void CheckBoxJSFunctions()
-
{
-
StringBuilder strScript = new StringBuilder();
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckAll(checkAllBox)");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var ChkState = checkAllBox.checked;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if(e.type == 'checkbox' && e.name.indexOf('Id') != -1)");
-
strScript.Append("e.checked = ChkState ;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScriptCheckAll"))
-
this.RegisterClientScriptBlock("clientScriptCheckAll", strScript.ToString());
-
-
//------------------------------------------------------------------------------
-
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckChanged()");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var boolAllChecked;");
-
strScript.Append("boolAllChecked=true;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e = frm.elements[i];");
-
strScript.Append("if (e.type =='checkbox' && e.name.indexOf('Id') != -1 )");
-
strScript.Append("if(e.checked == false)");
-
strScript.Append("{");
-
strScript.Append("boolAllChecked = false;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if ( e.type =='checkbox' && e.name.indexOf('checkAll') != -1 )");
-
strScript.Append("{");
-
strScript.Append("if( boolAllChecked == false)");
-
strScript.Append("e.checked = false ;");
-
strScript.Append("else if(boolAllChecked == true)");
-
strScript.Append("e.checked = true;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScriptCheckChanged"))
-
this.RegisterClientScriptBlock("clientScriptCheckChanged", strScript.ToString());
-
}
-
-
// Function to get the selected checkbox values
-
private string GetSelected()
-
{
-
// Initially set the values to Blank
-
h_chkSelected.Value = "";
-
-
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
Label lbl = (Label)di.FindControl("RowId");
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ",";
-
}
-
}
-
-
// If no checkbox selected, do not proceed further
-
string strDataToSend = "";
-
if (h_chkSelected.Value != "")
-
{
-
//Strip off the last comma before sending to stored proc
-
strDataToSend = h_chkSelected.Value;
-
strDataToSend = strDataToSend.Substring(0, strDataToSend.Length - 1);
-
}
-
return strDataToSend;
-
}
-
-
protected void btnDelete_Click(object sender, EventArgs e)
-
{
-
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
}
-
-
public virtual void DeleteRows()<---------//ErrorNo:2
-
{
-
int rowIndex;
-
}
-
-
}
Thanx in advance......
prem
25 2682
Hello premprakashbhati,
From my understanding this is a "Classic" ASP forum. Your question should be posted in the ".NET" section under Forums/Programming Languages/.NET
Sorry,
CroCrew~
Good Morning.....
i want to have checkboxes in my gridview ...by checking it ,i can delete the row/rows.... so i have written code and got 2 Errors ...can anyone solve it...
Error:1...'gridview.RetrieveItems()': not all code paths return a value ,and
Error:2...No overload for method 'DeleteRows' takes '1' arguments
Here is my aspx.cs Code: - using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Collections;
-
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.Text;
-
using System.Data.SqlClient;
-
-
public partial class gridview : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
// Include the JavaScript functions during Form_Load() itself.
-
CheckBoxJSFunctions();
-
-
if (!Page.IsPostBack)
-
{
-
PopulateGrid();
-
}
-
}
-
private static DataTable RetrieveItems()<------ //ErrorNo:1
-
{
-
-
//Create a connection to the SQL Server; modify the connection string for your environment
-
//SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Truste d_Connection=yes");
-
SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;UID=my User;PWD=myPassword;");
-
-
// Create a Command object, and then set the connection.
-
// The following SQL statements check whether a GetAuthorsByLastName
-
// stored procedure already exists.
-
SqlCommand MyCommand = new SqlCommand("select * from sysobjects where id = object_id(N'GetPosts')" +
-
" and OBJECTPROPERTY(id, N'IsProcedure') = 1", MyConnection);
-
-
// Set the command type that you will run.
-
MyCommand.CommandType = CommandType.Text;
-
-
// Open the connection.
-
MyCommand.Connection.Open();
-
-
// Run the SQL statement, and then get the returned rows to the DataReader.
-
SqlDataReader MyDataReader = MyCommand.ExecuteReader();
-
-
// If any rows are returned, the stored procedure that you are trying
-
// to create already exists. Therefore, try to create the stored procedure
-
// only if it does not exist.
-
if(!MyDataReader.Read())
-
{
-
MyCommand.CommandText = "create procedure GetPosts" +
-
" (@date varchar(40),@title varchar(50),(@author varchar(40),@categories varchar(50),(@tags varchar(40),@comments varchar(50)(@status varchar(40), select * from Posts where" +
-
" Date like @date,PTittle like @title,Author like @author,Category like @categories,Tags like @tags,TotalComments like comments,PStatus like @status; select @RowCount=@@ROWCOUNT";
-
MyDataReader.Close();
-
MyCommand.ExecuteNonQuery();
-
}
-
else
-
{
-
MyDataReader.Close();
-
}
-
-
MyCommand.Dispose(); //Dispose of the Command object.
-
MyConnection.Close(); //Close the connection.
-
}
-
-
//Call the stored procedure in the Click event of the btnGetAuthors button,
-
-
//DataTable Posts = new DataTable();
-
//DataTable dt = new DataTable();
-
//dt.Load(dr);
-
//if (dt != null)
-
//{
-
// Posts = dt;
-
//}
-
//return dt;
-
-
private void PopulateGrid()
-
{
-
// RetrieveItems() is a user defined function that calls the stored proc to retrieve data from the database.
-
DataTable dtItems = RetrieveItems();
-
GridView1.DataSource = dtItems;
-
GridView1.DataBind();
-
}
-
-
private void CheckBoxJSFunctions()
-
{
-
StringBuilder strScript = new StringBuilder();
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckAll(checkAllBox)");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var ChkState = checkAllBox.checked;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if(e.type == 'checkbox' && e.name.indexOf('Id') != -1)");
-
strScript.Append("e.checked = ChkState ;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScript CheckAll"))
-
this.RegisterClientScriptBlock("clientScriptCheckA ll", strScript.ToString());
-
-
//------------------------------------------------------------------------------
-
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckChanged()");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var boolAllChecked;");
-
strScript.Append("boolAllChecked=true;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e = frm.elements[i];");
-
strScript.Append("if (e.type =='checkbox' && e.name.indexOf('Id') != -1 )");
-
strScript.Append("if(e.checked == false)");
-
strScript.Append("{");
-
strScript.Append("boolAllChecked = false;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if ( e.type =='checkbox' && e.name.indexOf('checkAll') != -1 )");
-
strScript.Append("{");
-
strScript.Append("if( boolAllChecked == false)");
-
strScript.Append("e.checked = false ;");
-
strScript.Append("else if(boolAllChecked == true)");
-
strScript.Append("e.checked = true;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScript CheckChanged"))
-
this.RegisterClientScriptBlock("clientScriptCheckC hanged", strScript.ToString());
-
}
-
-
// Function to get the selected checkbox values
-
private string GetSelected()
-
{
-
// Initially set the values to Blank
-
h_chkSelected.Value = "";
-
-
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
Label lbl = (Label)di.FindControl("RowId");
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ",";
-
}
-
}
-
-
// If no checkbox selected, do not proceed further
-
string strDataToSend = "";
-
if (h_chkSelected.Value != "")
-
{
-
//Strip off the last comma before sending to stored proc
-
strDataToSend = h_chkSelected.Value;
-
strDataToSend = strDataToSend.Substring(0, strDataToSend.Length - 1);
-
}
-
return strDataToSend;
-
}
-
-
protected void btnDelete_Click(object sender, EventArgs e)
-
{
-
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
}
-
-
public virtual void DeleteRows()<---------//ErrorNo:2
-
{
-
int rowIndex;
-
}
-
-
}
-
Thanx in advance......
prem
Error1: Where you are returning the DataTable? In cases where you don't return a data type at the scope of the method, you will receive such errors.
Error2: I guess it is DeleteRow not DeleteRows.
Good Afternoon Everyone..
I have got Error in my code for getting GridviewCheckBoxes checked and deleted....
when iam clicking Delete_Click() iam getting Error stating..................
Error:object reference not set to an instance of the object...
Note:GetSelected Function is used in Delete_Click() function Code:aspx.cs - // Function to get the selected checkbox values
-
private string GetSelected()
-
{
-
// Initially set the values to Blank
-
h_chkSelected.Value = "";
-
-
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
Label lbl = new Label();
-
-
lbl = (Label)di.FindControl("Title");
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ","; //Error location
-
}
-
}
-
-
// If no checkbox selected, do not proceed further
-
string strDataToSend = "";
-
if (h_chkSelected.Value != "")
-
{
-
//Strip off the last comma before sending to stored proc
-
strDataToSend = h_chkSelected.Value;
-
strDataToSend = strDataToSend.Substring(0, strDataToSend.Length - 1);
-
}
-
return strDataToSend;
-
}
Do this
MyClass myObj = new MyClass();
myObj.MyMethod();
thanx for ur cooperation...
but still, iam getting no errors in my app but ia ma not able to delete the row on Delete_Click()
i think some thing wrong in DeleteRow() function....
if wrong plz help me out ..
thanx again... -
protected void btnDelete_Click(object sender, EventArgs e)
-
{
-
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
}
-
-
-
-
public static void DeleteRows(string PTittle)
-
{
-
-
SqlConnection MyConnection = new SqlConnection("server=(local);database=BlogDatabase;UID=sa;PWD=meritinfo;");
-
SqlCommand MyCommand = new SqlCommand("Delete from Posts where PTittle=@PTittle ", MyConnection);
-
MyCommand.Parameters.Add(new SqlParameter("@PTittle", PTittle));
-
MyCommand.CommandType = CommandType.Text;
-
MyCommand.Connection.Open();
-
SqlDataReader dr = null;
-
dr = MyCommand.ExecuteReader();
-
//DataTable Posts = new DataTable();
-
//DataTable dt = new DataTable();
-
//dt.Load(dr);
-
//if (dt != null)
-
//{
-
// Posts = dt;
-
//}
-
-
-
}
good evening..
iam implementing checkboxes in gridview and wanna delete gridview row on checked in Delete_Click() event...
till now iam getting no errors in my app but still iam not able to delete the row/rows..
i think the below DeleteRow() is not properly builded...
plz help me out...
thanx............ - protected void btnDelete_Click(object sender, EventArgs e)
-
{
-
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
}
-
-
-
-
-
public static void DeleteRows(string PTittle)
-
{
-
-
SqlConnection MyConnection = new SqlConnection("server=(local);database=BlogDatabase;UID=sa;PWD=meritinfo;");
-
SqlCommand MyCommand = new SqlCommand("Delete from Posts where PTittle=@PTittle ", MyConnection);
-
MyCommand.Parameters.Add(new SqlParameter("@PTittle", PTittle));
-
MyCommand.CommandType = CommandType.Text;
-
MyCommand.Connection.Open();
-
SqlDataReader dr = null;
-
dr = MyCommand.ExecuteReader();
-
//DataTable Posts = new DataTable();
-
//DataTable dt = new DataTable();
-
//dt.Load(dr);
-
//if (dt != null)
-
//{
-
// Posts = dt;
-
//}
-
-
-
}
Try putting this after you have opened the connection:
MyCommand.ExecuteNonQuery();
Check the query Delete from Posts where PTittle= @PTittle. But you are collecting the row numbers instead of titles in your GetSelected method.
Use the following code to achieve your goal. -
-
public static void DeleteRows(string PID)
-
{
-
SqlConnection MyConnection = new SqlConnection("server=CPC477-0039;database=BlogDatabas e;UID=sa;PWD=cart;");
-
SqlCommand MyCommand = new SqlCommand("Delete from Posts where PID=@PID", MyConnection);
-
MyCommand.Parameters.Add(new SqlParameter("@PID", PID));
-
MyCommand.CommandType = CommandType.Text;
-
MyCommand.Connection.Open();
-
-
MyCommand.ExecuteNonQuery();
-
}
-
-
dear,
still not working..
do u think DeleteRow(_______) function parameter is not proper accordingly because here we need selectedrow/rows.
as u said In myTable ,no PID is exist
MyTable fields are ---->>>>Date,PTittle,Category,Tags,TotalComments,PStat us,Author.
Please don't double (or quadruple, in your case) post your questions. If you made a mistake and need to change your question, you can click the Edit button to edit your post, or you can post your corrections in a reply to your original thread. If you can't find your original thread, click the " My Subscriptions" link near the top of the page. If you feel that your question has been overlooked, post a reply to it to "bump" it back to the top of the forum. We ask that you do this only once every 24 hours.
So there is no reason to double post. It makes it hard on the Experts and you to keep track of what help you've already been given.
Threads merged.
Also,
Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.
This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
Please use [CODE] [/CODE] tags in future.
MODERATOR
@premprakashbhati
Did you make the changes suggested by dmj07: @dmj07
Your DeleteRow() method accepts single String parameter "PTitle".
Your variable names are confusing....you are calling a function that is named "GetSelectedRows" and passing this result to your Delete method. Make sure that this "GetSelectedRows" method properly returns a "PTitle" (and consider renaming things so that this is obvious).
The other thing is: have you checked your database to see if it has deleted?
Please post any modified code so that we can see what you are currently working with.
Also please remember to use code tags when posting code snippets.
-Frinny
goodmorning Frinovale....
as u said use return of Getselected() function which is strDataToSend obviously it is a collection of selected rows in a gridview.
i wanna know that if we give strDataToSend parameter to the DeleteRows(_______) then what is the select statement for delete....
According to me Delete statement takes only the Database Table field name as parameter...
so plz show me the way....
thanx... Code:aspx.cs - using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Collections;
-
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.Text;
-
using System.Data.SqlClient;
-
-
public partial class gridview : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
// Include the JavaScript functions during Form_Load() itself.
-
CheckBoxJSFunctions();
-
-
if (!Page.IsPostBack)
-
{
-
PopulateGrid();
-
}
-
}
-
private static DataTable RetrieveItems()
-
{
-
SqlConnection MyConnection = new SqlConnection("server=Merit4;database=BlogDatabase;UID=sa;PWD=meritinfo;");
-
SqlCommand MyCommand = new SqlCommand("select Date,PTittle as Title,Author,Category as Categories,Tags,TotalComments as Comments,PStatus as Status from Posts", MyConnection);
-
MyCommand.CommandType = CommandType.Text;
-
MyCommand.Connection.Open();
-
SqlDataReader dr = null;
-
dr = MyCommand.ExecuteReader();
-
DataTable Posts = new DataTable();
-
DataTable dt = new DataTable();
-
dt.Load(dr);
-
if (dt != null)
-
{
-
Posts = dt;
-
}
-
return dt;
-
}
-
-
-
private void PopulateGrid()
-
{
-
// RetrieveItems() is a user defined function that calls the stored proc to retrieve data from the database.
-
DataTable dtItems = RetrieveItems();
-
GridView1.DataSource = dtItems;
-
GridView1.DataBind();
-
}
-
-
-
private void CheckBoxJSFunctions()
-
{
-
StringBuilder strScript = new StringBuilder();
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckAll(checkAllBox)");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var ChkState = checkAllBox.checked;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if(e.type == 'checkbox' && e.name.indexOf('Id') != -1)");
-
strScript.Append("e.checked = ChkState ;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScriptCheckAll"))
-
this.RegisterClientScriptBlock("clientScriptCheckAll", strScript.ToString());
-
-
//------------------------------------------------------------------------------
-
-
strScript.Append("<script language=JavaScript>");
-
strScript.Append("function CheckChanged()");
-
strScript.Append("{");
-
strScript.Append("var frm = document.form1;");
-
strScript.Append("var boolAllChecked;");
-
strScript.Append("boolAllChecked=true;");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e = frm.elements[i];");
-
strScript.Append("if (e.type =='checkbox' && e.name.indexOf('Id') != -1 )");
-
strScript.Append("if(e.checked == false)");
-
strScript.Append("{");
-
strScript.Append("boolAllChecked = false;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("for(i=0; i<frm.length; i++)");
-
strScript.Append("{");
-
strScript.Append("e=frm.elements[i];");
-
strScript.Append("if ( e.type =='checkbox' && e.name.indexOf('checkAll') != -1 )");
-
strScript.Append("{");
-
strScript.Append("if( boolAllChecked == false)");
-
strScript.Append("e.checked = false ;");
-
strScript.Append("else if(boolAllChecked == true)");
-
strScript.Append("e.checked = true;");
-
strScript.Append("break;");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("}");
-
strScript.Append("</script>");
-
if (!this.IsClientScriptBlockRegistered("clientScriptCheckChanged"))
-
this.RegisterClientScriptBlock("clientScriptCheckChanged", strScript.ToString());
-
}
-
-
-
-
// Function to get the selected checkbox values
-
private string GetSelected()
-
{
-
// Initially set the values to Blank
-
h_chkSelected.Value = "";
-
-
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
-
Label lbl = (Label)di.FindControl("Title");
-
lbl = new Label();
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ",";
-
}
-
}
-
-
// If no checkbox selected, do not proceed further
-
string strDataToSend = "";
-
if (h_chkSelected.Value != "")
-
{
-
//Strip off the last comma before sending to stored proc
-
strDataToSend = h_chkSelected.Value;
-
strDataToSend = strDataToSend.Substring(0, strDataToSend.Length - 1);
-
}
-
return strDataToSend;
-
}
-
-
-
-
protected void btnDelete_Click(object sender, EventArgs e)
-
{
-
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
}
-
-
public static void DeleteRows(string strDataToSend)
-
{
-
-
SqlConnection MyConnection = new SqlConnection("server=(local);database=BlogDatabase;UID=sa;PWD=meritinfo;");
-
SqlCommand MyCommand = new SqlCommand("Delete from Posts where PTittle In ("+@PTittle+")",MyConnection);
-
MyCommand.Parameters.Add(new SqlParameter("@PTittle", PTittle));
-
MyCommand.CommandType = CommandType.Text;
-
MyCommand.Connection = MyConnection;
-
MyConnection.Open();
-
//MyCommand.Connection.Open();
-
//SqlDataReader dr = null;
-
//dr = MyCommand.ExecuteReader();
-
//dr.Close();
-
MyCommand.ExecuteNonQuery();
-
//DataTable Posts = new DataTable();
-
//DataTable dt = new DataTable();
-
//dt.Load(dr);
-
//if (dt != null)
-
//{
-
// Posts = dt;
-
//}
-
-
-
}
-
-
-
}
-
-
-
Code:.aspx -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="gridview" %>
-
<!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>Grid View and Check Boxes</title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<input id="h_chkSelected" name="h_chkSelected" type="hidden" runat="server" value=""/>
-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Verdana" Font-Size="12px" Width="987px" CellPadding="3" EmptyDataText="No Items Found.">
-
<RowStyle BackColor="Beige" HorizontalAlign="Left" Height="15px" />
-
<HeaderStyle BackColor="Control" Font-Names="Verdana" Font-Size="12px" ForeColor="Black" HorizontalAlign="Left" Height="20px" />
-
<Columns>
-
<asp:TemplateField>
-
<ItemStyle Width="5%"></ItemStyle>
-
<HeaderTemplate>
-
<input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll"/>
-
</HeaderTemplate>
-
<ItemTemplate>
-
<input type="checkbox" runat="server" id="chkBoxId" name="chkBoxId" onclick="CheckChanged();" />
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField Visible="False">
-
<%--<ItemTemplate>
-
<asp:Label id="RowId" Text='<% #DataBinder.Eval(Container.DataItem, "ItemId") %>' runat="server" />
-
</ItemTemplate>--%>
-
</asp:TemplateField>
-
<asp:BoundField DataField="Date" HeaderText="Date" />
-
<asp:BoundField DataField="Title" HeaderText="Title" />
-
<asp:BoundField DataField="Author" HeaderText="Author" />
-
<asp:BoundField DataField="Categories" HeaderText="Categories" />
-
<asp:BoundField DataField="Tags" HeaderText="Tags" />
-
<asp:BoundField DataField="Comments" HeaderText="Comments" />
-
<asp:BoundField DataField="Status" HeaderText="Status" />
-
<asp:TemplateField>
-
<ItemStyle Width="5%"></ItemStyle>
-
-
<ItemTemplate>
-
<input type="button" fontsize="XX-Small" value="Publish" runat="server" id="btnpublish" name="btnpub" onclick="CheckChanged();" />
-
</ItemTemplate>
-
</asp:TemplateField>
-
</Columns>
-
<PagerStyle BackColor="Control" ForeColor="Black" Height="10px" HorizontalAlign="Center" />
-
</asp:GridView>
-
-
-
-
                                                      
-
<table style="z-index: 102; left: 19px; width: 282px; position: absolute; top: 520px">
-
<tr>
-
<td style="width: 100px">
-
</td>
-
</tr>
-
</table>
-
-
-
-
-
-
-
-
-
                                                                                                                                                                                                                                                                                                                                                                       
-
<asp:Button ID="btnDelete" runat="server" Font-Names="Verdana" Font-Size="Small"
-
Style="z-index: 101; left: 73px; position: absolute; top: 523px" Text="Delete Selected" OnClick="btnDelete_Click" />
-
</div>
-
</form>
-
</body>
-
</html>
As pointed by insertAlias, do wrap your code in [code] blocks. Plz click the # button on your reply window(Prob sits at the extreme right on the top).The cursor will be in between the CODE blocks, then do paste your code.
Remove the following line of code from your GetSelected function. lbl = new Label(); After finding the control, you are creating new Lable whose text property will be EMPTY with the above statment.
I think, now you can solve your issue easily.
dear goodevening..
as u said i have removed lbl=new label() from code in my GetSelected() function
iam getting Error: Null Reference Exception: Object reference not set to an instance of an object.
so plz see to it
Where it is causing the exception?
Did you check the string passed to DeleteRows method?
Do post the strDataToSend along with the exception where it occurred.
Try this: - Label lbl = (Label)di.FindControl("Title");
-
//lbl = new Label();
-
if(lbl != null)
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ",";
-
Mr.Ramk,
At
Label lbl=(Label)di.FindControl("Title")
Description:
di = DataGridRow which is selected
Title = Fieldname in DataGridView
Here i am getting Row No's in di object but it cannot be initialize to the object lbl ,it is showing null value..
so plz see to it..
I have asked you once to use [code] tags. This is not a request, it is a requirement of this site.
You need to start surrounding your code with [CODE] code goes here [/CODE] like that.
MODERATOR
@insertAlias
The FindControl method takes the Control ID as parameter to search the controls on the form.
Does your "Title" is ID of your control?
Do check it once again.
GoodMorning,,
yes Title is a label control in the grid.... -
private string GetSelected()
-
{
-
// Initially set the values to Blank
-
h_chkSelected.Value = "";
-
-
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
Label lbl = (Label)di.FindControl("Title") ;
-
if (lbl != null)
-
{
-
-
h_chkSelected.Value = h_chkSelected.Value + lbl.Text + ",";
-
-
}
-
}
-
}
-
-
// If no checkbox selected, do not proceed further
-
-
string strDataToSend = "";
-
if (h_chkSelected.Value != "")
-
{
-
//Strip off the last comma before sending to stored proc
-
-
strDataToSend = h_chkSelected.Value;
-
strDataToSend = strDataToSend.Substring(0, strDataToSend.Length - 1);
-
}
-
return strDataToSend ;
-
}
-
-
Can you show me the return string of GetSelected()? Is it ,,, something like that?
strDataToSend is the return string...
actually i want to know how this string is given as parameter to Delete_Click()
and how in delete command statement....that is Delete from Tablename where
________..... is declared...
I know the procedure when we use SqlDatasource but Dynamically in coding how we have to place that strDataToSend in delete command...like -
-
-
string title = Convert.ToString (GridView1.DataKeys[di.RowIndex].Value);
-
-
// Pass the value of the selected title to the Delete command.
-
-
SqlDataSource1.DeleteParameters["PTittle"].DefaultValue = title.ToString();
-
SqlDataSource1.Delete();
-
-
-
-
DELETE
-
FROM TABLE_NAME
-
WHERE TITLE IN ('Title1', 'Title2', 'Title3')
-
Hi Premprakashbhati,
I'm sorry that I was unable to respond sooner but I have a feeling that you need to change your code around a bit.
What you need to do is delete the rows that are selected by:
1. Loop through all of the rows selected
2. When you find a row that is selected, call your DeleteRows method and pass it the "PTittle" of the selected row.
So instead of: -
// This variable contains the selected rows in the format 1,2,3
-
string SelectedRows = GetSelected();
-
// User defined function that calls the stored proc to delete the selected items
-
DeleteRows(SelectedRows);
-
You would do something like: -
foreach (GridViewRow di in GridView1.Rows)
-
{
-
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBoxId");
-
-
if (chkBx != null && chkBx.Checked)
-
{
-
//Here find your PTittle value and call your DeleteRows() function passing it the PTittle value.
-
-
}
-
}
-Frinny
Does the DELETE query helped you in?Hope it's +ve!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Faybert |
last post by:
Hello, and Thanks in advance for any light you might shed on my
troubles.
I'm trying to setup a series of checkboxes, or a checkboxlist to
control the results that are shown on a gridview...
|
by: ssims |
last post by:
I've got a GridView that's sorted by a stored procedure with
ROW_NUMBER:
PROCEDURE dbo.GetCalendarsByStatusIDPaged
(
@startRowIndex int,
@maximumRows int,
@statusID int
)
AS
|
by: CSharpguy |
last post by:
I have code that exports a grid view to Excel, but when I put that code in my
content page of my master pages, I get an error,
gridview must be in a <form runat=servertag. So how can I export a...
|
by: mike7510uk |
last post by:
Hi, I am using a gridview with a templatefield containing a checkbox.
I want to update the database with a 1 or 0 depending on if a checkbox
is checked or unchecked (then use the 1 or 0 later on...
|
by: shapper |
last post by:
Hello,
I need to loop though each row in a GridView and if the checkbox is a
Template Field is checked I want to display the value of an invisible
column named "LevelName".
I tried everything...
|
by: beton3000 |
last post by:
Hello!
I'm building a GridView using code to add template fields, because
there is a random number of columns in result set from database. I'm
using a class with ITemplate interface for defining...
|
by: vijeberal |
last post by:
Hi all,
I am working on a GridView and there exist a Checkbox control .. iplaced another checkbox in header also i have written a javascript for selection of checkboxex fro e.g if you click on...
|
by: gh |
last post by:
I am using VS 2008. I have a gridview with several columns that are
checkboxes. The data for each column is either a 1 or 0. When I try to
view the page in the browser I get an error "Specified...
|
by: idan |
last post by:
Here's my data: (Table: EmployeePlan)
EmpID Plan Status
abc123 PlanA A
abc123 PlanB D
abc123 PlanC A
xyz789 PlanA D
xyz789 PlanB D
xyz789 PlanC...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |