Advertisement

06.23.2008 at 05:38PM PDT, ID: 23509580
[x]
Attachment Details

How to run an sql statement when a user clicks on a button

Asked by cesemj in Microsoft Visual Basic.Net, Programming for ASP.NET, Visual Studio .NET 2005

Tags: Microsoft, visual studio 2005

I have a aspx page with a gridview control.  I added a check box (cbxInactive) to the gridview as a template field.  When the user clicks a checkbox next to record that should be inactive and clicks the button, Remove Inctive Records, the script below will set the inactive field for each record to 1 , copy the record from the tblActive to tbl Archive, delete the record form tblActive, and rebind the grid so the Active records are displayed.  

Currently, the end-user tells me the records to mark inactive and I manually run the sql scripts to remove the inactive, which of course is a poor way to do things.

I listed what I have done below and have been searching the internet, but have not found an example or article yet.  Please share any ideas or excamples that can help me.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
The quires I use manually:
INSERT INTO  tblArchive(OrderID, UnitSerialNumber, Class, Type, Status, Title, Inactive)
 
Select       OrderID AS OrderID, UnitSerialNumber AS UnitSerialNumber, Class AS Class, Type As Type, Status As Status, Title AS Title, Inactive AS Inactive
 
FROM         tblActive
WHERE        Inactive = 1; 
 
DELETE FROM tblActive
      WHERE Inactive = 1
 
aspx Page Code
---------------
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
 
        DataKeyNames="OrderID" DataSourceID="AllJobs">
 
        <Columns>
 
            <asp:HyperLinkField DataNavigateUrlFields="OrderID" DataNavigateUrlFormatString="frmTMAPListings.aspx?OrderID={0}"
 
                Text="Order" />
 
            <asp:TemplateField HeaderText="Order">
 
                <ItemTemplate>
 
                    <asp:CheckBox ID="ProcessJob" runat="server" />
 
                </ItemTemplate>
 
            </asp:TemplateField>
 
            <asp:BoundField DataField="OrderID" HeaderText="Order ID" ReadOnly="True" SortExpression="OrderID" />
 
            <asp:BoundField DataField="UnitSerialNumber" HeaderText="UnitSerialNumber" SortExpression="UnitSerialNumber" />
           
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
 
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
 
            <asp:BoundField DataField="Status" HeaderText="status" SortExpression="Status" />
 
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
 
            <asp:TemplateField HeaderText="Inactive">
 
 
                <ItemTemplate>
 
                    <asp:CheckBox ID="cbxInactive1" runat="server" Checked='<%# Bind("Inactive") %>' />
 
                </ItemTemplate>
 
            </asp:TemplateField>
 
       </Columns>
 
    </asp:GridView>
 
    <asp:SqlDataSource ID="AllJobs" runat="server" ConnectionString="<%$ ConnectionStrings:JobConnectionString %>"
 
        SelectCommand="SELECT OrderID, UnitSerialNumber, Class, Type, Status, Title, Inactive FROM tblActive WHERE (Class = @Class) ORDER BY Class, Type" ProviderName="<%$ ConnectionStrings:JobsConnectionString.ProviderName %>">
 
        <SelectParameters>
 
            <asp:ControlParameter ControlID="DropDownList1" Name="Class" PropertyName="SelectedValue" />
 
        </SelectParameters>
 
    </asp:SqlDataSource>
<asp:LinkButton ID="btnRmvActiveRecords" runat="server" Visible="False">LinkButton</asp:LinkButton><br />
 
Code Behind Page
----------------
 
Protected Sub btnRmvActiveRecords_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRmvActiveRecords.Click
 
        Dim atLeastOneRowSeleted As Boolean = False
 
        Dim ids As New StringCollection
 
 
 
        ' Iterate through the Rows 
 
        For Each row As GridViewRow In GridView1.Rows
 
            ' Access the CheckBox
 
            Dim cb As CheckBox = row.FindControl("cbxInactive")
 
            If cb IsNot Nothing AndAlso cb.Checked Then
 
                atLeastOneRowSeleted = True
 
 
 
                ' First, get the OrderID for the selected row
 
                Dim OrderID As Integer = _
 
                    Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
 
                ids.Add(OrderID.ToString)
 
            End If
 
        Next
 
 
 
        'Execute SQL 
 
 
        Dim cmd As New SqlClient.SqlCommand()
 
 
 
        cmd.Connection = New SqlConnection(Me.AllJobs.ConnectionString)
 
        cmd.CommandText = "INSERT INTO  tblArchive(OrderID, UnitSerialNumber, Class, Type, Status, Title, Inactive) Select OrderID AS OrderID, UnitSerialNumber AS UnitSerialNumber, Class AS Class, Type As Type, Status As Status, Title AS Title, Inactive AS Inactive FROM tblActive WHERE Inactive = 1; DELETE FROM tblActive WHERE Inactive = 1"
  
 
        cmd.Connection.Open()
 
        cmd.Connection.Close()
[+][-]06.23.2008 at 08:06PM PDT, ID: 21852227

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 09:14PM PDT, ID: 21852475

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 09:28PM PDT, ID: 21852520

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 09:54PM PDT, ID: 21852600

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 10:49PM PDT, ID: 21852765

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 10:15AM PDT, ID: 21858020

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 02:05PM PDT, ID: 21860394

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 12:02AM PDT, ID: 21863157

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 07:12AM PDT, ID: 21865772

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 08:25PM PDT, ID: 21871642

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 07:05PM PDT, ID: 21880520

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 09:44PM PDT, ID: 21881028

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 10:28PM PDT, ID: 21881132

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 10:33PM PDT, ID: 21881138

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 10:42PM PDT, ID: 21881164

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2008 at 10:49PM PDT, ID: 21881190

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.27.2008 at 02:11AM PDT, ID: 21881921

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.27.2008 at 02:49PM PDT, ID: 21887978

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.27.2008 at 02:55PM PDT, ID: 21888003

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.27.2008 at 10:13PM PDT, ID: 21889388

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Programming for ASP.NET, Visual Studio .NET 2005
Tags: Microsoft, visual studio 2005
Sign Up Now!
Solution Provided By: anuragal
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.30.2008 at 07:39PM PDT, ID: 21904671

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628