how to delete data on botton click???

Posted by Shailesh21235 under ASP.NET on 6/8/2012 | Points: 10 | Views : 1695 | Status : [Member] | Replies : 8
IN DAL
public int DataDelete(string sql)
{
SqlCommand _objcomm = null;
DatabaseConnection _objconn = new DatabaseConnection();
_objcomm = _objconn.GetCommand();
_objcomm.Connection.Open();
_objcomm.CommandText = sql;
int result = _objcomm.ExecuteNonQuery();
return result;
_objcomm.Connection.Close();
}
IN C# part
protected void btnSubmitdb_Click(object sender, EventArgs e)
{
DatabaseConnection objconn = new DatabaseConnection();
string sql = "Insert into Table_Details values('"+txtNamedb.Text+"','"+ txtAgedb.Text+"','"+ txtEmaildb.Text+"')";
objconn.DataInsert(sql);
}
protected void btnDeletedb_Click(object sender, EventArgs e)
{
DatabaseConnection objconn = new DatabaseConnection();
string sql = "DELETE FROM Table_Details WHERE Id=@Id";
objconn.DataDelete(sql);
}
the insert and select action are being performed.I have use the similar function for insert and delete..Does it creates aProblem??? The datas are displayed in grid view then how to delete the entire row from gridview and from database????




Responses

Posted by: Santhi on: 6/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Refer this link:
http://www.dotnetspark.com/kb/643-how-to-editupdatedelete-gridview.aspx



Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 6/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Again call the gridview bind data function. so that gridview will bind the new values after the delete event occured.

Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 6/9/2012 [Member] Starter | Points: 25

Up
0
Down
hey shanti,
when i select the select button it shows the datas is gridview...
when the delete button is selected then which value will it delete???how to delete the desired data???

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 6/11/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Can you explain it clearly.

Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 6/11/2012 [Member] Starter | Points: 25

Up
0
Down
hi,
when i press the delete botton which datas from database will be deleted??? what is the query for delete???

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 6/11/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

How this can be done...
Which data will be deleted from the gridview..

What is your need..
Whether you have to delete a particular record from the gridview which has been selected by the user.
Am i right..
If this is right means you must refer the link..


Thanks & Regards,
Santhi .V

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 6/11/2012 [Member] Starter | Points: 25

Up
0
Down
protected void gdvSelect_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DatabaseConnection objconn = new DatabaseConnection();
GridViewRow row = (GridViewRow)gdvSelect.Rows[e.RowIndex];
Label lbldelete = (Label)row.FindControl("lblid");
string sql = "delete * from Table_Details where rowid=" + lbldelete.Text + "";
objconn.DataDelete(sql);
}
i wrote this cide in aspx.cs page and the string is passed to another function in class databaseconnection is DAL...

public int DataDelete(string sql)
{
SqlCommand _objcomm = null;
DatabaseConnection _objconn = new DatabaseConnection();
_objcomm = _objconn.GetCommand();
_objcomm.Connection.Open();
_objcomm.CommandText = sql;
int result = _objcomm.ExecuteNonQuery();
return result;
_objcomm.Connection.Close();
}

than it gave a error System.NullReferenceException: Object reference not set to an instance of an object?? THat string sql is passed to another function datadelete... What is the mistake????

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 6/11/2012 [Member] Starter | Points: 25

Up
0
Down
<asp:GridView ID="gdvSelect" runat="server" AutoGenerateColumns="false"
AutoGenerateDeleteButton="True"
onselectedindexchanged="gdvSelect_SelectedIndexChanged"
onrowdeleting="gdvSelect_RowDeleting" >
<Columns>
<asp:BoundField DataField="Id" ReadOnly="true" HeaderText="Id" />
<asp:BoundField DataField="Name" ReadOnly="true" HeaderText="Name" />
<asp:BoundField DataField="Age" ReadOnly="true" HeaderText="Age" />
<asp:BoundField DataField="Email" ReadOnly="true" HeaderText="Email" />
</Columns>
</asp:GridView>

protected void gdvSelect_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DatabaseConnection objconn = new DatabaseConnection();
SqlCommand objcomm = null;
GridViewRow row = (GridViewRow)gdvSelect.Rows[e.RowIndex];
Label lbldelete = (Label)row.FindControl("Id");
objcomm.CommandText = "delete from Table_Details where rowid=" + lbldelete.Text + "";
objcomm.Connection.Open();
objcomm.ExecuteNonQuery();
objcomm.Connection.Close();
}

why is this not working???

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response