views:

729

answers:

3

I have a dataset that points to a View in my SQL database, and a gridview that uses this dataset to populate itself. I of course want the rows editable, so I added Edit and Delete functionality to the gridview - standard method (not custom buttons)

working with the Update Query now... For some reason it didn't auto-generate the update statement (Probably cause the dataset points to a view rather than a table i'm guessing) so I have to create it myself. No problem - I select the dataset, choose properties, and choose UpdateQuery.

Here is my query:

UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)

The parameter source is set to "Form" and I have the name of each parameter listed.

Here is the relevant code from the aspx page:

        <asp:SqlDataSource ID="dsHardware" runat="server"
        ConnectionString="<%$ ConnectionStrings:RequestFormsConnectionString %>"
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT * FROM [vHardware]" 
        UpdateCommand="UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)" 
        InsertCommand="INSERT INTO [vHardware] ([HardwareID], [TypeID], [Description], [Name], [Cost], [Weight], [Image], [TypeDesc], [TypeName], [TypeTypeID]) VALUES (@HardwareID, @TypeID, @Description, @Name, @Cost, @Weight, @Image, @TypeDesc, @TypeName, @TypeTypeID)" 
        DeleteCommand="DELETE From tblHardware WHERE HardwareID = @HardwareID">
        <UpdateParameters>
            <asp:FormParameter FormField="TypeID" Name="TypeID" />
            <asp:FormParameter FormField="Name" Name="Name" />
            <asp:FormParameter FormField="Description" Name="Description" />
            <asp:FormParameter FormField="Cost" Name="Cost" />
            <asp:FormParameter FormField="Weight" Name="Weight" />
            <asp:FormParameter FormField="Image" Name="Image" />
            <asp:FormParameter FormField="HardwareID" Name="HardwareID" />
        </UpdateParameters>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="HardwareID,TypeTypeID"
        DataSourceID="dsHardware">
        <Columns>
            <asp:BoundField DataField="HardwareID" HeaderText="HardwareID" InsertVisible="False"
                ReadOnly="True" SortExpression="HardwareID" />
            <asp:BoundField DataField="TypeID" HeaderText="TypeID" SortExpression="TypeID" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
            <asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />
            <asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
            <asp:BoundField DataField="TypeDesc" HeaderText="TypeDesc" SortExpression="TypeDesc" />
            <asp:BoundField DataField="TypeName" HeaderText="TypeName" SortExpression="TypeName" />
            <asp:BoundField DataField="TypeTypeID" HeaderText="TypeTypeID" InsertVisible="False"
                ReadOnly="True" SortExpression="TypeTypeID" />
        </Columns>
    </asp:GridView>

So how is this all supposed to work? Apologies I am new to ASP.NET - previous classic ASP developer.

EDIT: Sorry I should be more clear. When attempting an Update, I receive the error "@Hardware needs to be defined". Not sure why as it looks plenty defined to me. Also, there is NO code in my .cs file - I assume I need something there, but not sure what exactly.

A: 

Just a wild guess here, might be me doing it wrong but it DOES work - try binding the HardwareID parameter to the gridview?

<asp:ControlParameter> should help you out.

<asp:ControlParameter ControlID="GridView1" Name="HardwareID" 

                        PropertyName="SelectedValue" Type="Int32" />

Kindof.

Might complain about something else though.

[edit] Oh, that should go inside your "UpdateParameters" and "DeleteParameters".

[Edit, once more] You will only need code in your .cs if you plan on checking the data.

MrZombie
Nah, I'm still getting "Must declare the variable '@HardwareID'"
Kolten
A: 

The above code doesn't close the asp:SQLDataSource?

Or was that just snipped out in the copy paste?

yes, just snipped.
Kolten
A: 

Ok I guess some erraneous entries (that I have no idea what they do) entered into my code somehow. Namely, this line:

OldValuesParameterFormatString="original_{0}"

...re-made the gridview to use standard methods as well. Works like a charm!

If anyone is reading this, how can I validate the data?

Kolten