GridView Help Need

Discussion in 'ASP.NET / ASP.NET Core' started by Detter, Feb 12, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. First please forgive me I am a newbe to ASP and I have been watching every video and reading every thing I can find on the Gridview Control and I can't find an example for inserting and new row into a sql database. I have the Insert button enalbed and on the objectdatasource I have an Insert Query that works in the Query Builder but when I click on the Insert Button it doesn't create my new record.



    this is the code



    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="ObjectDataSource1" DataTextField="ID" DataValueField="ID">


    </asp:DropDownList>


    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetID" TypeName="DataSet1TableAdapters.GetIDTableAdapter"></asp:ObjectDataSource>


    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="Primary" DataSourceID="ObjectDataSource2" ForeColor="Black" GridLines="None">


    <Columns>


    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" ShowInsertButton="true"/>


    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />


    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />


    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />


    <asp:BoundField DataField="Discount" HeaderText="Discount" SortExpression="Discount" />


    <asp:BoundField DataField="Retail" HeaderText="Retail" SortExpression="Retail" />


    <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />


    <asp:BoundField DataField="Catagory" HeaderText="Catagory" SortExpression="Catagory" />


    <asp:CheckBoxField DataField="Instock" HeaderText="Instock" SortExpression="Instock" />


    <asp:BoundField DataField="ManufactureID" HeaderText="ManufactureID" SortExpression="ManufactureID" />


    <asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />


    <asp:BoundField DataField="Descript" HeaderText="Descript" SortExpression="Descript" />


    <asp:BoundField DataField="Primary" HeaderText="Primary" InsertVisible="False" ReadOnly="True" SortExpression="Primary" />


    </Columns>


    <FooterStyle BackColor="Tan" />


    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />


    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />


    <HeaderStyle BackColor="Tan" Font-Bold="True" />


    <AlternatingRowStyle BackColor="PaleGoldenrod" />


    </asp:GridView>


    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataProducts" TypeName="DataSet1TableAdapters.ProductsTableAdapter" UpdateMethod="Update">


    <DeleteParameters>


    <asp:parameter Name="Original_Primary" Type="Int32" />


    </DeleteParameters>


    <UpdateParameters>


    <asp:parameter Name="ID" Type="Int32" />


    <asp:parameter Name="Name" Type="String" />


    <asp:parameter Name="Image" Type="String" />


    <asp:parameter Name="Price" Type="Double" />


    <asp:parameter Name="Discount" Type="Double" />


    <asp:parameter Name="Retail" Type="Double" />


    <asp:parameter Name="Qty" Type="Int32" />


    <asp:parameter Name="Catagory" Type="String" />


    <asp:parameter Name="Instock" Type="Boolean" />


    <asp:parameter Name="ManufactureID" Type="Int32" />


    <asp:parameter Name="Descript" Type="String" />


    <asp:parameter Name="Original_Primary" Type="Int32" />


    </UpdateParameters>


    <SelectParameters>


    <asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="SelectedValue" Type="Int32" />


    </SelectParameters>


    <InsertParameters>


    <asp:parameter Name="ID" Type="Int32" />


    <asp:parameter Name="Name" Type="String" />


    <asp:parameter Name="Image" Type="String" />


    <asp:parameter Name="Price" Type="Double" />


    <asp:parameter Name="Discount" Type="Double" />


    <asp:parameter Name="Retail" Type="Double" />


    <asp:parameter Name="Qty" Type="Int32" />


    <asp:parameter Name="Catagory" Type="String" />


    <asp:parameter Name="Instock" Type="Boolean" />


    <asp:parameter Name="ManufactureID" Type="Int32" />


    <asp:parameter Name="Descript" Type="String" />


    </InsertParameters>


    </asp:ObjectDataSource>
    I created this code while watching the Learning Video "hilo_intro_final"
    It all works except they gave no example on Inserting a row. If any one could help me I would be very greatful. I think I have to pass the field imformation to the insert parameters but I am not sure how to do that.

    Thanks
     
  2. hmm

    not sure, but did you allow updating and adding on your datasource? I think what you are needing is more in the datasource than the grid itself.
     
  3. ...Are you sure you want a GridView for this?
    Try the ListView, you can do more and format it with HTML as needed.
    Salute,
    Mark
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page