Solved

Insert on DataGridView is not working on my ASP.Net web page

Posted on 2010-11-10
5
405 Views
Last Modified: 2012-08-13
I have the following definition in a form on my webpage.  The code puts a datagridview on the page and populates the grid from a table in my database.  Each row has an Edit, Delete option.  If Edit is chosen, the user can change the fields in the datagridview.  When finished with the modifications, the Update option is clicked at which time the changes are made to the database.  This works as I expected and want.  The last row in the datagridview is to insert a new record.  When I try and do the Insert, a new record is added but the fields are blank.  I copied this format from another page that does the same thing with a different table and that jpage works fine.  I'm probably just missing something but can't figure out what it is.  Any help would be greatly appreciated.
<asp:GridView ID="BereavementFacilitatorsGridView" runat="server" DataSourceID="BereavementFacilitators" 

                    AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ID" 

                    ShowFooter="True" EmptyDataText="No Facilitators Found">

                    <Columns>

                        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 

                            ReadOnly="True" SortExpression="ID" Visible="False" />

                        <asp:BoundField DataField="Category" HeaderText="Category" 

                            SortExpression="Category" Visible="False" />

                        <asp:TemplateField HeaderText="Contact" SortExpression="Contact">

                            <EditItemTemplate>

                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Contact") %>'></asp:TextBox>

                            </EditItemTemplate>

                            <FooterTemplate>

                                <asp:TextBox ID="NewFacilitatorTB" runat="server" Width="218px"></asp:TextBox>

                            </FooterTemplate>

                            <ItemTemplate>

                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Contact") %>'></asp:Label>

                            </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Phone" SortExpression="Phone">

                            <EditItemTemplate>

                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>

                            </EditItemTemplate>

                            <FooterTemplate>

                                <asp:TextBox ID="NewFacilitatorPhoneTB" runat="server" Width="218px"></asp:TextBox>

                            </FooterTemplate>

                            <ItemTemplate>

                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>

                            </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField ShowHeader="False">

                            <EditItemTemplate>

                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 

                                    CommandName="Update" Text="Update"></asp:LinkButton>

                                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 

                                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>

                            </EditItemTemplate>

                            <ItemTemplate>

                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 

                                    CommandName="Edit" Text="Edit"></asp:LinkButton>

                                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 

                                    CommandName="Delete" Text="Delete"></asp:LinkButton>

                            </ItemTemplate>

                            <FooterTemplate>

                                <asp:Button ID="AddNewFacilitator" runat="server" CausesValidation="false"

                                    CommandName="Insert" Text="Insert" OnClick="AddNewFacilitator_Click"></asp:Button>

                            </FooterTemplate>    

                         </asp:TemplateField>

                    </Columns>

                    </asp:GridView>

                <asp:AccessDataSource ID="BereavementFacilitators" runat="server" 

                    DataFile="~/App_Data/ABVM.mdb" 

                    SelectCommand="SELECT [ID], [Category], [Contact], [Phone] FROM [Bereavement] WHERE ([Category] = ?)"

                    DeleteCommand="DELETE FROM [Bereavement] WHERE [ID] = ?" 

                    InsertCommand="INSERT INTO [Bereavement] ([Category], [Contact], [Phone]) VALUES (?, ?, ?)" 

                    UpdateCommand="UPDATE [Bereavement] SET [Contact] = ?, [Phone] = ? WHERE [ID] = ?">

                    <SelectParameters>

                        <asp:Parameter DefaultValue="Facilitators" Name="Category" Type="String" />

                    </SelectParameters>

                    <DeleteParameters>

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

                    </DeleteParameters>

                    <UpdateParameters>

                        <asp:Parameter Name="Contact" Type="String" />

                        <asp:Parameter Name="Phone" Type="String" />

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

                    </UpdateParameters>

                    <InsertParameters>

                        <asp:Parameter DefaultValue="Facilitators" Name="Category" Type="String" />

                        <asp:Parameter Name="Contact" Type="String" />

                        <asp:Parameter Name="Phone" Type="String" />

                    </InsertParameters>

               </asp:AccessDataSource>

Open in new window

0
Comment
Question by:dyarosh
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 34110910
by the code you hjave posted i see that upoin insert new record the following function is being called: AddNewFacilitator_Click

can u post its implementation?
0
 

Author Comment

by:dyarosh
ID: 34110988
Here is the function:

Public Sub AddNewFacilitator_Click(ByVal sender As Object, ByVal e As System.EventArgs)
BereavementFacilitators.Insert()
End Sub
0
 

Assisted Solution

by:dyarosh
dyarosh earned 0 total points
ID: 34111810
I finally discovered the problem.  I need to set the parameter fields on the Inserting event for the data source.  Here is the code that solved the problem:

Protected Sub BereavementFacilitators_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles BereavementFacilitators.Inserting
Dim CT As TextBox = BereavementFacilitatorsGridView.FooterRow.FindControl("NewFacilitatorTB")
Dim PH As TextBox = BereavementFacilitatorsGridView.FooterRow.FindControl("NewFacilitatorPhoneTB")
e.Command.Parameters("Category").Value = "Facilitators"
e.Command.Parameters("Contact").Value = CT.Text
e.Command.Parameters("Phone").Value = PH.Text
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34112039
great job, 10x for the points.
0
 

Author Closing Comment

by:dyarosh
ID: 34143336
Thank you for pointing me in the right direction to find the problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Class Library Dynamics For Connectstring Information 2 18
ASP.NET reading ATOM 2 51
Host asp.net pages 5 25
SQL Exceptions 3 35
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now