Isaac
asked on
Gridview - Enable Inserting
In ASP.NET I click checkbox that says "Generate INSERT, UPDATE, and DELETE statements."
In my gridview, I see 'Enable Editing, Enable Deleting, etc.
The only thing I don't see is 'Enable Insert'. Am I doing something wrong. How can I enable insert?
Thanks.
In my gridview, I see 'Enable Editing, Enable Deleting, etc.
The only thing I don't see is 'Enable Insert'. Am I doing something wrong. How can I enable insert?
Thanks.
Gridview doesn't support Inserting, you'll have to add that support yourself.
Here's one example to do this:
http://fredrik.nsquared2.com/viewpost.aspx?PostID=155
Here's one example to do this:
http://fredrik.nsquared2.com/viewpost.aspx?PostID=155
ASKER
I found something very similar to what I want and I followed the steps exactly but nothing happens when I click "insert". I don't even get errors.
http://www.dotnetbips.com/articles/c1e0ca90-5f5d-47aa-a739-492b562e810a.aspx
http://www.dotnetbips.com/articles/c1e0ca90-5f5d-47aa-a739-492b562e810a.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="ID_KEY" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" AllowSorting="True"
AllowPaging="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="DUE_DATE" HeaderText="DUE_DATE"
SortExpression="DUE_DATE" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION"
SortExpression="DESCRIPTION" />
<asp:BoundField DataField="ID_KEY" HeaderText="ID_KEY" ReadOnly="True"
SortExpression="ID_KEY" Visible="False" />
<asp:ButtonField CommandName="Insert" Text="Insert" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<EmptyDataTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
</asp:DetailsView>
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:oracleConnectionString %>"
ProviderName="<%$ ConnectionStrings:oracleConnectionString.ProviderName %>"
SelectCommand="SELECT "DUE_DATE", "DESCRIPTION", "ID_KEY" FROM "OGC_DATE_DUE_INFO_TBL" WHERE ("CASE_NUMBER" = :CASE_NUMBER)"
DeleteCommand="DELETE FROM "OGC_DATE_DUE_INFO_TBL" WHERE "ID_KEY" = :ID_KEY"
InsertCommand="INSERT INTO "OGC_DATE_DUE_INFO_TBL" ("DUE_DATE", "DESCRIPTION", "ID_KEY") VALUES (:DUE_DATE, :DESCRIPTION, :ID_KEY)"
UpdateCommand="UPDATE "OGC_DATE_DUE_INFO_TBL" SET "DUE_DATE" = :DUE_DATE, "DESCRIPTION" = :DESCRIPTION WHERE "ID_KEY" = :ID_KEY">
<SelectParameters>
<asp:ControlParameter ControlID="ed_caseNumber" Name="CASE_NUMBER"
PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ID_KEY" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DUE_DATE" Type="DateTime" />
<asp:Parameter Name="DESCRIPTION" Type="String" />
<asp:Parameter Name="ID_KEY" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DUE_DATE" Type="DateTime" />
<asp:Parameter Name="DESCRIPTION" Type="String" />
<asp:Parameter Name="ID_KEY" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
Is this an exact paste of the example?
I see a lot of "& q u o t ; " (spaces between to demonstrate) that WILL crash the script...
Are you running the GridView in an UpdatePanel? (remove the Update Panel and you'll see the errors...)
Espavo
I see a lot of "& q u o t ; " (spaces between to demonstrate) that WILL crash the script...
Are you running the GridView in an UpdatePanel? (remove the Update Panel and you'll see the errors...)
Espavo
ASKER
No. That's what my code created in VS as I followed the example.
Also, I'm not running in an updatePanel. I am in a tabcontainer though. Maybe that's the problem.
Also, I'm not running in an updatePanel. I am in a tabcontainer though. Maybe that's the problem.
Do you have any records displaying in your GridView?
ASKER
Yes I do.
Where did you get the example from? (My reason for asking is that a GridView doesn't support Insert... which is why I do the Footer work-around on it... so it's strange to see that someone has it working...)
ASKER
ASKER
I applied your idea to my code but when I run it, my button does not show up.
what am I doing wrong?
what am I doing wrong?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="ID_KEY" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" AllowSorting="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="DUE_DATE" HeaderText="DUE_DATE"
SortExpression="DUE_DATE" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION"
SortExpression="DESCRIPTION" />
<asp:BoundField DataField="ID_KEY" HeaderText="ID_KEY" ReadOnly="True"
SortExpression="ID_KEY" Visible="False" />
<asp:TemplateField ShowHeader="False">
<FooterTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Insert" Text="Insert" OnClick="Insert_Click"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used what you have and I added what's below. I get no errors but it does'nt work either.
What am I doing wrong?
Thx.
What am I doing wrong?
Thx.
public void insert_click(object sender, EventArgs e)
{
TextBox dueDate = GridView1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;
TextBox description = GridView1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;
SqlDataSource1.InsertParameters["DUE_DATE"].DefaultValue = dueDate.Text;
SqlDataSource1.InsertParameters["DESCRIPTION"].DefaultValue = description.Text;
SqlDataSource1.Insert();
}
Well, to start off with, I see that you have three input parameters... (but you are only passing two...)
<InsertParameters>
<asp:Parameter Name="DUE_DATE" Type="DateTime" />
<asp:Parameter Name="DESCRIPTION" Type="String" />
<asp:Parameter Name="ID_KEY" Type="Decimal" />
</InsertParameters>
ASKER
Well, ID_KEY is just my primary key. My backend is ORACLE so how I do I call the sequence that I have already created to increment ID_KEY?
ASKER
Also, where would I paste your <inputParameter> code? Right after the grid, in the grid?
ASKER
Now I get this error
System.NullReferenceExcept ion was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_qw2lacs-"
StackTrace:
at edit_case.Insert_Click(Obj ect sender, EventArgs e) in d:\My Documents\Visual Studio 2008\WebSites\OGC\edit_cas e.aspx.cs: line 484
at System.Web.UI.WebControls. LinkButton .OnClick(E ventArgs e)
at System.Web.UI.WebControls. LinkButton .RaisePost BackEvent( String eventArgument)
at System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
InnerException:
points to this line: SqlDataSource1.InsertParam eters["DUE _DATE"].De faultValue = dueDate.Text;
System.NullReferenceExcept
Message="Object reference not set to an instance of an object."
Source="App_Web_qw2lacs-"
StackTrace:
at edit_case.Insert_Click(Obj
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
InnerException:
points to this line: SqlDataSource1.InsertParam
public void Insert_Click(object sender, EventArgs e)
{
TextBox dueDate = GridView1.FooterRow.FindControl("txtDue_Date") as TextBox;
TextBox description = GridView1.FooterRow.FindControl("txtDescription") as TextBox;
SqlDataSource1.InsertParameters["DUE_DATE"].DefaultValue = dueDate.Text;
SqlDataSource1.InsertParameters["DESCRIPTION"].DefaultValue = description.Text;
SqlDataSource1.Insert();
}
The gridview is not designed for inserting. The detailsview control is the one to use.
Chris
Chris
Here's an example of how I do it...
Espavo
Open in new window