Hi all,

I have a field named 'UserDeleted' which is set to false by default. Records shouldnt be deleted from the database but the field Userdeleted should be set to true when user deletes a particular record.

when i click on delete button a gridview is shown with delete commanfield and other details. If i press this delte button then the data gets delted from the databse, which i dnt want. instead whenever i select a row the Userdeleted field should be set to true and a message should be displayed tht the user is deleted.


Thanks in advance.

again. I am not to clear about what exactly you are saying. But if you create a DataGrid it usually create its won functions for Add, Update and Delete (the DataGrid CRUD methods). you can do one of 2 things:

1. remove the ability to delete using the datagrid controls
2. edit the control methods themselves to do what you want them to do.

If that was your problem I hope it helps. But usually if you post code it makes things alot easier to help you out with your problem.

8-D

Hi mike..

Am using visual web developer 2005 express and sql server management studio express for developing an intranet application.

u r rite datagrid creates itz own add,update and delete functions.
Whenever a user selects a row in the datagrid, then that should be logically deleted. .i.e. UserDeleted field in the database should be set to 'true' which was 'false' by default before.

Hope u got it what i want to tell..


Code goes here:

<asp:Content ID="Content1" ContentPlaceHolderID="conthld" Runat="Server">
<asp:GridView ID="GridView1" runat="server" style="z-index: 101; left: 218px; position: absolute; top: 224px" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="LoginId" DataSourceID="SqlDataSource1" GridLines="Vertical" OnSelectedIndexChanging="select_click" >
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:CommandField SelectText="Delete" ShowSelectButton="True" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="LoginId" HeaderText="LoginId" ReadOnly="True" SortExpression="LoginId" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="MobileNo" HeaderText="MobileNo" SortExpression="MobileNo" />
<asp:BoundField DataField="UserAccess" HeaderText="UserAccess" SortExpression="UserAccess" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cashflow %>"
SelectCommand="SELECT UserName, LoginId, Email, MobileNo, UserAccess FROM Login_Tab WHERE (UserDeleted = 'False')" UpdateCommand="UPDATE Login_Tab SET UserDeleted = 'False' WHERE (LoginId = LoginId)">
</asp:SqlDataSource>

</asp:Content>

<script runat=server >
Dim constr As String
Dim cmd As SqlCommand
Dim con As SqlConnection
Dim sql As String
Dim re As SqlDataReader

Protected Sub select_click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs)


sql = "update Login_Tab set UserDeleted='True' where LoginId= @LoginId "

Try
con = New SqlConnection(constr)
cmd = New SqlCommand(sql, con)


con.Open()
cmd.ExecuteNonQuery()
lblinfo.Visible = True
lblinfo.Text = "Record Deleted"

Catch ex As Exception
lblinfo.Visible = True
lblinfo.Text = ex.Message
Finally
con.Close()
End Try

End Sub

</script>


May be the problem seems small, but am ignorant about the code.

The query tht is highlighted with red color is giving problem i think.
Thanks a lot for the reply.

Please let me know where i have gone wrong.

I am still not 100% on what you are trying to do. sorry I am really trying to understand but i found something that caught my eye. Maybe it will help

SelectCommand="SELECT UserName, LoginId, Email, MobileNo, UserAccess FROM Login_Tab WHERE (UserDeleted = 'False')" UpdateCommand="UPDATE Login_Tab SET UserDeleted = 'False' WHERE (LoginId = LoginId)">
    </asp: sqlDataSource>
  
</asp:Content>

<script runat=server >
    Dim constr As String
    Dim cmd As SqlCommand
    Dim con As SqlConnection
    Dim sql As String
    Dim re As SqlDataReader
    
Protected Sub select_click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs)
       
        
         sql = "update Login_Tab set UserDeleted='True' where LoginId= @LoginId "

If you notice you have 2 update commands which are doing the opposite of each other, one is setting the value to true, the other is setting the value to false. Maybe that is what it is suppose to do, but it does not seem to fit. Hope this helps.

Hi mike,

Thanks for the reply i will try out tht.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.