0

Insert

  • converted all the fields to a template field
  • enabled the footer for the grid
  • added text-boxes to the footer template

enter image description here

here is the code aspx.cs

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlDataSource1.Insert();           
    }

here is my .aspx code for the insert command

 InsertCommand="INSERT INTO [Net Weight Tracking] (Date, [Unit UPC Base Item], [Item (Optional)], [Preset Number], [Product Group], Shift, [Rotation Code], BBD, [Operator Name], Supervisor, [Production Line], [Bagger Number], [Start Time], [Stop Time], [Under Counts], [Label Wt on Pkg (g)], [Machine Tare Wt (g)], [Actual Tare Wt (g)], [Verify Target Wt (g)], [Total Count (Proper)], [Mean Gross (g)], [Rptd Mean Net (g)], [Std Dev (g)], [Max (g)], [Min (g)], [TNE (g)], Comments, Field1, Field2, Field3) VALUES (@date, @unit, @item, @preset, @prodgroup, @shift, @rotationcode, @bbd, @operatorname, @supervisorname, @productionline, @baggernumber, @starttime, @stoptime, @undercount, @labelwt, @machine, @actual, @verify, @totalcount, @meangross, @rptd, @std, @mx, @min, @tne, @comments, @field, @field2, @field3)" 
<InsertParameters>
            <asp:Parameter Name="Date" />
            <asp:Parameter Name="UnitUPCBaseItem" />
            <asp:Parameter Name="Item" />
            <asp:Parameter Name="Preset" />
            <asp:Parameter Name="Product" />
            <asp:Parameter Name="Shift" />
            <asp:Parameter Name="Rotation" />
            <asp:Parameter Name="BBD" />
            <asp:Parameter Name="Operator" />
            <asp:Parameter Name="Supervisor" />
            <asp:Parameter Name="Production" />
            <asp:Parameter Name="Bagger" />
            <asp:Parameter Name="Start" />
            <asp:Parameter Name="Stop" />
            <asp:Parameter Name="Under" />
            <asp:Parameter Name="Label" />
            <asp:Parameter Name="Machine" />
            <asp:Parameter Name="Actual" />
            <asp:Parameter Name="Verify" />
            <asp:Parameter Name="Total" />
            <asp:Parameter Name="Mean" />
            <asp:Parameter Name="Rptd" />
            <asp:Parameter Name="Std" />
            <asp:Parameter Name="Max" />
            <asp:Parameter Name="Min" />
            <asp:Parameter Name="TNE" />
            <asp:Parameter Name="Comments" />
            <asp:Parameter Name="Field1" />
            <asp:Parameter Name="Field2" />
            <asp:Parameter Name="Field3" />
            <asp:Parameter Name="date" />
            <asp:Parameter Name="unit" />
            <asp:Parameter Name="item" />
            <asp:Parameter Name="preset" />
            <asp:Parameter Name="prodgroup" />
            <asp:Parameter Name="shift" />
            <asp:Parameter Name="rotationcode" />
            <asp:Parameter Name="bbd" />
            <asp:Parameter Name="operatorname" />
            <asp:Parameter Name="supervisorname" />
            <asp:Parameter Name="productionline" />
            <asp:Parameter Name="baggernumber" />
            <asp:Parameter Name="starttime" />
            <asp:Parameter Name="stoptime" />
            <asp:Parameter Name="undercount" />
            <asp:Parameter Name="labelwt" />
            <asp:Parameter Name="machine" />
            <asp:Parameter Name="actual" />
            <asp:Parameter Name="verify" />
            <asp:Parameter Name="totalcount" />
            <asp:Parameter Name="meangross" />
            <asp:Parameter Name="rptd" />
            <asp:Parameter Name="std" />
            <asp:Parameter Name="mx" />
            <asp:Parameter Name="min" />
            <asp:Parameter Name="tne" />
            <asp:Parameter Name="comments" />
            <asp:Parameter Name="field" />
            <asp:Parameter Name="field2" />
            <asp:Parameter Name="field3" />
        </InsertParameters>

I enter all the data and click insert and it inserts an empty row in my sql table with all null values, i want the data to be inserted in the database and reflect on the grid as well

I figured that the gridview is not getting the values so i tried this on the insert still not working:

 <InsertParameters>
                 <asp:FormParameter FormField="TextBox31" Name="Date" Type="DateTime" />
            <asp:FormParameter FormField="TextBox32" Name="Unit_UPC_Base_Item" 
                Type="String" />
            <asp:FormParameter FormField="TextBox33" Name="column1" Type="String" />

            <asp:FormParameter FormField="TextBox35" Name="Product_Group" Type="String" />
            <asp:FormParameter FormField="TextBox36" Name="Shift" Type="String" />
            <asp:FormParameter FormField="TextBox37" Name="Rotation_Code" Type="String" />
            <asp:FormParameter FormField="TextBox38" Name="BBD" Type="String" />
            <asp:FormParameter FormField="TextBox39" Name="Operator_Name" Type="String" />
            <asp:FormParameter FormField="TextBox40" Name="Supervisor" Type="String" />
            <asp:FormParameter FormField="TextBox41" Name="Production_Line" Type="String" />
            <asp:FormParameter FormField="TextBox42" Name="Bagger_Number" Type="Double" />
            <asp:FormParameter FormField="TextBox43" Name="Start_Time" Type="DateTime" />
            <asp:FormParameter FormField="TextBox44" Name="Stop_Time" Type="DateTime" />
            <asp:FormParameter FormField="TextBox45" Name="Under_Counts" Type="Double" />
            <asp:FormParameter FormField="TextBox46" Name="column2" Type="Double" />
            <asp:FormParameter FormField="TextBox47" Name="column3" Type="Double" />
            <asp:FormParameter FormField="TextBox48" Name="column4" Type="Double" />
            <asp:FormParameter FormField="TextBox49" Name="column5" Type="Double" />
            <asp:FormParameter FormField="TextBox50" Name="column6" Type="Double" />
            <asp:FormParameter FormField="TextBox51" Name="column7" Type="Double" />
            <asp:FormParameter FormField="TextBox52" Name="column8" Type="Double" />
            <asp:FormParameter FormField="TextBox53" Name="column9" Type="Double" />
            <asp:FormParameter FormField="TextBox54" Name="column10" Type="Double" />
            <asp:FormParameter FormField="TextBox55" Name="column11" Type="Double" />
            <asp:FormParameter FormField="TextBox56" Name="column12" Type="Double" />
            <asp:FormParameter FormField="TextBox57" Name="Comments" Type="String" />
            <asp:FormParameter FormField="TextBox58" Name="Field1" Type="DateTime" />
            <asp:FormParameter FormField="TextBox59" Name="Field2" Type="DateTime" />
            <asp:FormParameter FormField="TextBox60" Name="Field3" Type="String" />
            <asp:FormParameter FormField="TextBox34" Name="Preset_Number" />
</InsertParameters>

I have the rowcommand event and i thought clicking the button on the gridview will fire this event and it did but it is not able to get the values. enter image description here

PlZ Help

Solution Implemented but not working:

if (e.CommandName == "InsertRow")
            {
                SqlDataSource1.InsertParameters["Date"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox31")).Text;
                SqlDataSource1.InsertParameters["Unit_UPC_Base_Item"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox32")).Text;
                SqlDataSource1.InsertParameters["column1"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox33")).Text;
                SqlDataSource1.InsertParameters["Preset_Number"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox34")).Text;
                SqlDataSource1.InsertParameters["Product_Group"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox35")).Text;
                SqlDataSource1.InsertParameters["Shift"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox36")).Text;
                SqlDataSource1.InsertParameters["Rotation_Code"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox37")).Text;
                SqlDataSource1.InsertParameters["BBD"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox38")).Text;
                SqlDataSource1.InsertParameters["Operator_Name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox39")).Text;
                SqlDataSource1.InsertParameters["Supervisor"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox40")).Text;
                SqlDataSource1.InsertParameters["Production_Line"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox41")).Text;
                SqlDataSource1.InsertParameters["Bagger_Number"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox42")).Text;
                SqlDataSource1.InsertParameters["Start_Time"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox43")).Text;
                SqlDataSource1.InsertParameters["Stop_Time"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox44")).Text;
                SqlDataSource1.InsertParameters["Under_Counts"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox45")).Text;
                SqlDataSource1.InsertParameters["column2"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox46")).Text;
                SqlDataSource1.InsertParameters["column3"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox47")).Text;
                SqlDataSource1.InsertParameters["column4"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox48")).Text;
                SqlDataSource1.InsertParameters["column5"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox49")).Text;
                SqlDataSource1.InsertParameters["column6"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox50")).Text;
                SqlDataSource1.InsertParameters["column7"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox51")).Text;
                SqlDataSource1.InsertParameters["column8"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox52")).Text;
                SqlDataSource1.InsertParameters["column9"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox53")).Text;
                SqlDataSource1.InsertParameters["column10"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox54")).Text;
                SqlDataSource1.InsertParameters["column11"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox55")).Text;
                SqlDataSource1.InsertParameters["column12"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox56")).Text;
                SqlDataSource1.InsertParameters["Comments"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox57")).Text;
                SqlDataSource1.InsertParameters["Field1"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox58")).Text;
                SqlDataSource1.InsertParameters["Field2"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox59")).Text;
                SqlDataSource1.InsertParameters["Field3"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TextBox60")).Text;

                SqlDataSource1.Insert();

            }

As you said i am using simple parameters

  <InsertParameters>
                <asp:Parameter Name="Date" Type="DateTime" />
                <asp:Parameter Name="Unit_UPC_Base_Item" Type="String" />
                <asp:Parameter Name="column1" Type="String" />
                <asp:Parameter Name="Product_Group" Type="String" />
                <asp:Parameter Name="Shift" Type="String" />
                <asp:Parameter Name="Rotation_Code" Type="String" />
                <asp:Parameter Name="BBD" Type="String" />
                <asp:Parameter Name="Operator_Name" Type="String" />
                <asp:Parameter Name="Supervisor" Type="String" />
                <asp:Parameter Name="Production_Line" Type="String" />
                <asp:Parameter Name="Bagger_Number" Type="Double" />
                <asp:Parameter Name="Start_Time" Type="DateTime" />
                <asp:Parameter Name="Stop_Time" Type="DateTime" />
                <asp:Parameter Name="Under_Counts" Type="Double" />
                <asp:Parameter Name="column2" Type="Double" />
                <asp:Parameter Name="column3" Type="Double" />
                <asp:Parameter Name="column4" Type="Double" />
                <asp:Parameter Name="column5" Type="Double" />
                <asp:Parameter Name="column6" Type="Double" />
                <asp:Parameter Name="column7" Type="Double" />
                <asp:Parameter Name="column8" Type="Double" />
                <asp:Parameter Name="column9" Type="Double" />
                <asp:Parameter Name="column10" Type="Double" />
                <asp:Parameter Name="column11" Type="Double" />
                <asp:Parameter Name="column12" Type="Double" />
                <asp:Parameter Name="Comments" Type="String" />
                <asp:Parameter Name="Field1" Type="DateTime" />
                <asp:Parameter Name="Field2" Type="DateTime" />
                <asp:Parameter Name="Field3" Type="String" />
                <asp:Parameter Name="Preset_Number" />
            </InsertParameters>

1 Answer 1

1

Those aren't FormParameters, they're GridView fields which are not accessible through the form like that. If anything they're ControlParameters from the Gridview control. But you're not doing a standard Insert, so the simplest way to achieve the above is to do this"

  1. In the insert parameters box as you have shown above, change all the Form parameters to plain old Parameters
  2. Make sure your add button has a CommandName like "MyInsert" and NOT "Insert" the word "Insert" is reserved for standard gridview operations and pulling data from the footer, while common, is not standard.
  3. In the RowCommand event make sure you do a check against the e parameter for e.CommandName="MyInsert" before you go calling SqlDataSource1.Insert() function.
  4. Also before calling Insert() you have to assign all the fields to the SqlDataSource1.InsertParameters collection like this:

VB Code, but it should be a simple matter to convert

Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
  If e.CommandName = "MyInsert" Then
    SqlDataSource1.InsertParameters("Date").DefaultValue = foo("TextBox31")
    SqlDataSource1.InsertParameters("Unit_UPC_Base_Item").DefaultValue = foo("TextBox32")
    SqlDataSource1.InsertParameters("column1").DefaultValue = foo("TextBox33")
    .
    . (yada yada yada)
    .
    SqlDataSource1.Insert()
  End If
End Sub

' Helper function
Private Function foo( ControlID As String) As String
  ' add error checking
  Return CType(GridView1.FooterRow.FindControl(ControlID), TextBox).Text
End Function

  1. For the most part, type conversions are implicit, but you may have some text values that need to be explicitly converted.

I think that's about it - If I missed anything let me know and I'll update the post


Update 2015/03/02 ~16:31EST

When to check RowCommand event

When you use the Gridview context menu and you "Enable Selection" VisualStudio will insert a CommandField to the top of your list of Fields. When you edit your columns using the dialog you can see all the options that are available from a command field. Among them are: "Insert", "Select", "Delete", "Cancel" etc...these all coincide with the standard GridView operations. if you provide a SqlDataSource that has sql or stored proc for the Delete command, the delete option will be available, If you provide an sql or stored proc for Insert, then likewise Insert will be available

The AutoGenerated CommandName for each of these operations is "Select$N", "Delete$N", etc where N is the row number within the GridView command button. Each of these events has it's own Event handler of the appropriate name "RowSelecting", "RowSelected", "RowDeleting", "RowDeleted", etc.

In addition to raising these events the gridview also "Bubbles" these standard events up to the RowCommand event handler.

Now one thing you have to understand about gridview, and this is direct from Microsoft:

Inserting records into the data source is not directly supported by the GridView control. However, it is possible to insert records by using the GridView control in conjunction with the DetailsView or FormView control. For more information, see DetailsView or FormView, respectively.

That should tell you something. And it is counter intuitive because if you look at the CommandField within the Gidview you can plainly see the Insert Command. And using the footer row with a series of Textboxes is typically how a row insert is finessed for a GridView control.

Keep in mind that ALL commands pass through RowCommand even if they have their own handlers (Select, Cancel, Delete, etc), we just typically don't look for them there.

So, when do you Check RowCommand.CommandName? You check whenever you are trying to handle an event that is otherwise not supported by the Gridview.

In the case where you want to do an insert without using the GridView Insert command, you first disable Insert from the CommandField. It will be available because the connected SqlDataSource has an insert command defined.

You convert the CommandField to a TemplateField and edit the Template and add a Button to the FooterTemplate:

<FooterTemplate>
    <asp:Button ID="btnInsert" runat="server" CommandName="InsertRow" Text="Button" />
</FooterTemplate>

Now here is where you need to verify something.

If the button, as defined above, is triggering a CommandEvent with the CommandName of "Insert" then you either have Insert enabled (you shouldn't) or the fact that your custom CommandName begins with the word "Insert" it's causing a false firing of an Insert event.

If you recall any standard command as rendered by GridView is "Select$3" for example. This fires the Selecting/Selected events and bubbles to RowCommand with CommandName="Select".

I'm thinking that the button you created is causing the insert event to be fired. I could be wrong, but it's worth checking. In any case I'd try changing the CommandName to starts with something other than a reserved word.

Sign up to request clarification or add additional context in comments.

6 Comments

i tried the solution but its still not working, any other alternatives, i did what you suggested, before it was inserting an empty row now it is not even doing that.
I see you updated your original post. which event are you you running that code that starts with if e.CommandName == "InsertRow" ? An if you're still setting FormParameters in the SqlDatasource they must be basic simple <asp:Parameter ...>'s
i am running it from protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { } the row command was suppose to fire when the user clicks the button, i am not sure what is the issue with this grid view, when i created a details view and try to insert new records it is able to insert :( and they are simple parameters that are getting referenced under the row command.
if you step through the code and step into the row command event - are you seeing the textbox values? are they being assigned to the parameter default value?
Debugging was a great idea, it was going to if(e.commandname == InsertRow) and throwing itself out, for some strange reason it was not able to check that, i removed that and it set the values and the insert worked great, is it advisable to check for the e.commandname??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.