-
July 27th, 2009, 08:40 AM
#1
OleDbException ERROR, when trying to insert a row in a GridView
Hi all, (I work in VS2005, Access DB, C#)
I was trying to insert a row in a GridView and the following is the code. When I run this page, it doesn't display the existing data and in the footer row when I click on 'Insert' after entering the data, I get the following error:
========================================
OleDbException was unhandled by user code
Index or primary key cannot contain a Null value.
========================================
Please check the code and guide me where am I wrong?
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void lbInsert_Click(object sender, EventArgs e)
{
AccessDataSource1.Insert();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
((TextBox)GridView1.FooterRow.FindControl("empcodeIN")).Text = "";
((TextBox)GridView1.FooterRow.FindControl("PeriodIN")).Text = "";
}
protected void AccessDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
string emp = ((TextBox)GridView1.FooterRow.FindControl("empcodeIN")).Text;
string per = ((TextBox)GridView1.FooterRow.FindControl("PeriodIN")).Text;
e.InputParameters["empcode"] = emp;
e.InputParameters["Period"] = per;
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(Object sender, EventArgs e)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>View / Edit Roster Data: [ </strong><a href="Default.aspx"><strong>Home</strong></a><strong>]<br />
</strong>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="empcode" DataSourceID="AccessDataSource1" ForeColor="#333333" GridLines="None" ShowFooter="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCreated="GridView1_RowCreated">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ></asp:LinkButton>
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" ></asp:LinkButton>
<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" CommandName="Insert" ValidationGroup="addVal" OnClick="lbInsert_Click">Insert</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" OnClick="LinkButton2_Click"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpCode" SortExpression="empcode">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='' > </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="empcodeIN" runat="server" Text=''> </asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="empcodeIN" runat="server" Text=''> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="empcodeIN" ErrorMessage="Enter Empcode!" ValidationGroup="addVal"> </asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Period" SortExpression="Period">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text=''></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="PeriodIN" runat="server" Text='' Width="49px"> </asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="PeriodIN" runat="server" Text='' Width="49px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="PeriodIN" ErrorMessage="Enter Period!" ValidationGroup="addVal"> </asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Test.mdb"
DeleteCommand="DELETE FROM [lax] WHERE [empcode] = ?" InsertCommand="INSERT INTO [roster] ([empcode], [Period]) VALUES (?, ?)"
SelectCommand="SELECT * FROM [lax]" UpdateCommand="UPDATE [roster] SET [Period] = ? WHERE [empcode] = ?">
<DeleteParameters>
<asp:Parameter Name="empcode" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Period" Type="Int32" />
<asp:Parameter Name="empcode" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="empcode" Type="String" />
<asp:Parameter Name="Period" Type="Int32" />
</InsertParameters>
</asp:AccessDataSource>
</form>
</body>
</html>
Last edited by Shuja Ali; July 28th, 2009 at 03:37 PM.
Reason: Added code tags
Thank you for your support.
Best regards,
Lax
-
July 28th, 2009, 07:12 AM
#2
Re: OleDbException ERROR, when trying to insert a row in a GridView
The error message tells me that you are trying to insert a new row where an existing row already exists in the table with that primary key.
The easiest way is to place your code in a try catch block and in the catch block display an appropriate message based on the error caught.
If you find my answers helpful, dont forget to rate me
-
July 28th, 2009, 03:40 PM
#3
Re: OleDbException ERROR, when trying to insert a row in a GridView
I believe the error message says that you are trying to insert a null value in one of your columns that is either declared as primary key or has a not null constraint.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|