UPDATE command property for asp:SqlDataSource does not recognize parameter values - c#

The error occurs when the asp:ListView object tries to call the update command built in to the datasource. Specifically the error states:
Cannot insert the value NULL into column 'FirstName', table
'SqlCatalog.dbo.Members'; column does not allow nulls. UPDATE fails.
The statement has been terminated
I'm using the following code in the ItemUpdateEventHandler to verify NULL's are not being inserted into fields that are set to NOT NULL.
protected void Members_VerifyChanges(Object sender, ListViewItemUpdateEventArgs eArgs)
{
if(eArgs.NewValues["ID"] == null || eArgs.NewValues["FirstName"] == null || eArgs.NewValues["LastName"] == null)
{
eArgs.Cancel = true;
}
}
Here is the UPDATE command specified for the datasource:
UPDATE Members SET [ID]=original_ID, [Approved]=#App, [FirstName]=#First, [MiddleInitial]=#Mid, [LastName]=#Last WHERE ID=#original_ID
Since the event handler function does not cancel the UPDATE command I can be sure the NewValues dictionary has something other than NULL to be inserted into the FirstName field.
Here is the specific test case I have been working with.
Old Values
________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|_______Test_______|_______A_______|______Test_______|
New Values
________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|______Bruce_______|_______A_______|______Wayne______|
To me it seems like the NewValues Dictionary is not being added to the parameters but I don't know how I could check that. I've thought about manually creating and executing the update command in the event handler but that's a hack i'd prefer not to do.
Here is the asp markup for the SqlDataSource:
<asp:SqlDataSource ID="WaterFacultySqlDataSrc" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [ID], [Approved], [FirstName], [MiddleInitial], [LastName] FROM Members ORDER BY [ID] DESC"
InsertCommand="INSERT INTO [Members] (ID, Approved, FirstName, MiddleInitial, LastName VALUES (#ID, #App, #First, #Mid, #Last)"
UpdateCommand="UPDATE [Members] SET [ID]=#original_ID, [Approved]=#App, [FirstName]=#First, [MiddleInitial]=#Mid, [LastName]=#Last WHERE ID=#original_ID"
DeleteCommand="DELETE FROM [Members] WHERE ID=#original_ID">
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="App" Type="Boolean" />
<asp:Parameter Name="First" Type="String" />
<asp:Parameter Name="Mid" Type="Char" />
<asp:Parameter Name="Last" Type="String" />
</UpdateParameters>
/**For Brevity I've excluded the other commands parameters*/
and finally the markup for the ListView controller:
<asp:ListView ID="PendingUpdates" runat="server" DataKeyNames="ID" DataSourceID="WaterFacultySqlDataSrc" OnItemUpdating="Members_VerifyChanges">
<LayoutTemplate>
<table id="PendingUpdatesTable" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr id="TableHeaders" runat="server" style="background-color: #E0FFFF;color: #333333;">
<th id="ApproveOrDeny" runat="server">
Approve/Deny</th>
<th id="WF_ID_Header" runat="server">
ID</th>
<th id="Approved_Header" runat="server">
Approved</th>
<th id="First_Header" runat="server">
First Name</th>
<th id="Middle_Header" runat="server">
Initial</th>
<th id="Last_Header" runat="server">
Last Name</th>
</tr>
<tr ID="itemPlaceHolder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="Edit_Button" runat="server" CommandName="Edit" Text="Edit"/>
<asp:Button ID="Delete_Button" runat="server" CommandName="Delete" Text="Delete"/>
</td>
<td>
<asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
</td>
<td>
<asp:Label ID="Approve_Val" runat="server" Text='<%# Bind("Approved") %>' />
</td>
<td>
<asp:Label ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
</td>
<td>
<asp:Label ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
</td>
<td>
<asp:Label ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
</td>
</tr>
</ItemTemplate>
<EditTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="Save_Button" runat="server" CommandName="Update" Text="Save"/>
<asp:Button ID="Cancel_Button" runat="server" CommandName="Cancel" Text="Cancel"/>
</td>
<td>
<asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
</td>
<td>
<asp:checkBox ID="Approve_Val" runat="server" checked='<%# Bind("Approved") %>' />
</td>
<td>
<asp:textBox ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
</td>
<td>
<asp:textBox ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
</td>
<td>
<asp:textBox ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
</td>
</tr>
</EditTemplate>
</asp:ListView>

Not sure why but changing the names of 2 of the parameters fixed this error.
UPDATE Members SET [ID]=original_ID, [Approved]=#App, [FirstName]=#FirstName, [MiddleInitial]=#Mid, [LastName]=#LastName WHERE ID=#original_ID}
I'm guessing Microsoft SQL Server 2008 R2 is parsing First and Last as keywords to indicate the chronological First and Last parameters rather than the parameters named First and Last. I've not been able to confirm that, but I'll keep digging and post the answer when I find it.

Related

The name 'control' does not exist in the current context

I am having trouble with an error on a site I am working on that I've inherited, upon grabbing the source from the repository and building, then clearing out a few reference errors, I am still getting over 1000 instances of this error:
The name 'pnlDetails' does not exist in the current context (replace 'panelDetails' with any of my control names).
what this would seem to indicate is that the controls referenced server side are not declared on the page, or don't have runat=server in them, but in fact they do. it could also be a problem of the inherits attribute not matching, but it does. I have searched stackoverflow and seen this question before, though after trying some of the solutions mentioned, they did not help. I do not have any designer files for my pages. Below are some snippets of code from the aspx and aspx.cs pages. (some information redacted to protect client privacy)
My question is, why can't i reference my controls on the server side? 'paneldetails', 'rpAddresses' etc.?
default.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/org.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="directory_Default" %>
<%# Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=xxxx"
Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
<%# Register TagPrefix="nu" Namespace="Leap.NuCaptcha" Assembly="leapmarketing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphHead" Runat="Server">
<title>xxxx</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphBreadCrumb" Runat="Server">
<div id="breadCrumbFrame">Home > xxx</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphBody" Runat="Server">
<h3 class="sectionTitle"> xxx </h3>
<!--//////////// Begin Searchbox Panel ////////////-->
<asp:Panel ID="pnlSearchbox" Visible="true" runat="server" DefaultButton="lnkSearch">
<div class="info">
xxx xxx.
</div>
<h3 style="padding-bottom: 10px;">xxx:</h3>
<p>xxx xxx.</p>
<br />
<br />
<div class="addressLine">
<asp:Label ID="lblFirstName" CssClass="addressLabel" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" CssClass="addressEdit" runat="server"></asp:TextBox>
</div>
<div class="addressLine">
<asp:Label ID="lblLastName" CssClass="addressLabel" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" CssClass="addressEdit" runat="server"></asp:TextBox>
</div>
<div class="addressLine">
<asp:Label ID="lblCity" CssClass="addressLabel" runat="server" Text="City"></asp:Label>
<asp:TextBox ID="txtCity" CssClass="addressEdit" runat="server"></asp:TextBox>
</div>
<div class="addressLine">
<asp:Label ID="lblDistrict" CssClass="addressLabel" runat="server" Text="District"></asp:Label>
<asp:DropDownList ID="ddlDistrict" CssClass="addressDropDown" runat="server" DataSourceID="dsDistricts"
DataTextField="Name" DataValueField="DistrictID"
AppendDataBoundItems="True">
<asp:ListItem Value="">All Districts</asp:ListItem>
</asp:DropDownList>
</div>
<div class="addressLine">
<asp:Label ID="lblSpecialty" CssClass="addressLabel" runat="server" Text="Specialty"></asp:Label>
<asp:DropDownList ID="ddlSpecialty" CssClass="addressDropDown" runat="server"
DataSourceID="dsSpecialties" DataTextField="xxx_certification"
DataValueField="QualificationID" AppendDataBoundItems="True">
<asp:ListItem Value="">All xxx</asp:ListItem>
</asp:DropDownList>
</div>
<div class="addressLine">
<div class="addressLabel"> </div>
<asp:LinkButton ID="lnkSearch" CssClass="navButton" runat="server"
onclick="lnkSearch_Click">Search</asp:LinkButton>
</div>
</asp:Panel>
<asp:LinqDataSource ID="dsDistricts" runat="server"
ContextTypeName="org.Xrm.XrmDataContext" Select="new (DistrictID, Name)"
TableName="Districts">
</asp:LinqDataSource>
<asp:LinqDataSource ID="dsSpecialties" runat="server"
ContextTypeName="org.Xrm.XrmDataContext"
Select="new (QualificationID, xxx_certification)" TableName="Qualifications"
Where="EducationTypeID == #EducationTypeID">
<whereparameters>
<asp:Parameter DefaultValue="000" Name="EducationTypeID"
DbType="Guid" />
</whereparameters>
</asp:LinqDataSource>
<!--//////////// Begin Search Results Panel ////////////-->
<asp:Panel ID="pnlResults" Visible="false" runat="server">
<div id="ResultsTop" style="margin-bottom: 15px;">
<div id="ResultsLeft" style="float: left; padding-top: 5px;">
<h2>Search Results - <span class="BlackResults">
<asp:Label ID="lblCount" runat="server" Text=""></asp:Label></span></h2>
</div>
</div>
<br />
<br />
<br />
<asp:LinkButton ID="lnkNewSearch" CssClass="navButtonLarge" runat="server" OnClick="lnkNewSearch_Click">New Search</asp:LinkButton>
<br />
<br />
<div align="center">
<asp:DataPager ID="DataPager2" runat="server" PagedControlID="lvResults"
PageSize="25">
<fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="True" />
<asp:NumericPagerField ButtonCount="10" />
<asp:NextPreviousPagerField ShowLastPageButton="True"
ShowNextPageButton="True" ShowPreviousPageButton="False" />
</fields>
</asp:DataPager>
</div>
<br />
<asp:ListView ID="lvResults" runat="server" DataSourceID="dsSearchResults">
<itemtemplate>
<tr style="">
<td class="DirectoryItems">
<asp:LinkButton ID="lnkDetails" CommandArgument='<%# Eval("ID") %>' OnCommand="lnkDetails_Click" runat="server"><%# Eval("xxx") %></asp:LinkButton>
</td>
<td class="DirectoryItems">
<asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location") %>' />
</td>
<td class="DirectoryItems">
<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
</td>
</tr>
</itemtemplate>
<emptydatatemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</emptydatatemplate>
<layouttemplate>
<table runat="server" width="100%">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="border-collapse: collapse;" width="99%">
<tr runat="server" style="border-bottom:1px solid #828283; margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px;">
<td runat="server" width="40%">
<p class="DirectoryResultsHeader">xxx</p></td>
<td runat="server" width="30%">
<p class="DirectoryResultsHeader">Location</p></td>
<td runat="server" width="30%">
<p class="DirectoryResultsHeader">Status</p></td>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</layouttemplate>
</asp:ListView>
<br />
<br />
<div align="center">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvResults"
PageSize="25">
<fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="True" />
<asp:NumericPagerField ButtonCount="10" />
<asp:NextPreviousPagerField ShowLastPageButton="True"
ShowNextPageButton="True" ShowPreviousPageButton="False" />
</fields>
</asp:DataPager>
</div>
<asp:SqlDataSource ID="dsSearchResults" runat="server" OnSelected="dsSearchResults_Selected"
ConnectionString="<%$ ConnectionStrings:MSCRM %>" CancelSelectOnNullParameter="false"
SelectCommand="
SELECT * from sometable
">
<selectparameters>
<asp:ControlParameter ControlID="txtFirstName" DbType="String" Name="FirstName" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtLastName" DbType="String" Name="LastName" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtCity" DbType="String" Name="City" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="ddlDistrict" DbType="Guid" Name="District" ConvertEmptyStringToNull="true" />
<asp:ControlParameter ControlID="ddlSpecialty" DbType="Guid" Name="Specialty" ConvertEmptyStringToNull="true" />
</selectparameters>
</asp:SqlDataSource>
</asp:Panel>
<!--//////////// Begin Details Panel ////////////-->
<asp:Panel ID="pnlDetails" Visible="false" runat="server">
<h3>xxx Information</h3>
<br />
<asp:Repeater ID="rpDetails" runat="server" DataSourceID="dsDetails">
<itemtemplate>
<table border="0">
<tr>
<td width="125"><b>Given Name:</b></td>
<td><%# Eval("FirstName") %></td>
</tr>
<tr>
<td width="125"><b>Surname:</b></td>
<td><%# Eval("LastName") %></td>
</tr>
<tr>
<td width="125"><b>Gender:</b></td>
<td><%#Eval("Gender") %></td>
</tr>
<tr>
<td width="125"><b>Status:</b></td>
<td><%# Eval("Status") %></td>
</tr>
<asp:Panel ID="pnlSpecialty" runat="server" Visible='<%# Eval("Status").ToString() == "xxx" || Eval("Status").ToString() == "xxx" || Eval("Status").ToString() == "xxx" ? true : false %>'>
<tr>
<td width="125"><b>xxx:</b></td>
<td>
<asp:Repeater ID="xxx" runat="server" DataSourceID="xxx">
<ItemTemplate>
<%# Eval("xxx") %><br />
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</asp:Panel>
<tr>
<td width="125"><b>xxx:</b></td>
<td><%# Eval("xxx") %></td>
</tr>
</table>
</itemtemplate>
</asp:Repeater>
<br />
<br />
<asp:Repeater ID="rpAddresses" runat="server" DataSourceID="dsAddresses" OnItemDataBound="rpAddresses_DataBound">
<headertemplate>
<h3>xxx</h3><br />
</headertemplate>
<itemtemplate>
<table border="0">
<tr>
<td valign="top" width="125"><b><asp:Label ID="lblAddressType" runat="server" Text='<%# Eval("Type") %>' /></b></td>
<td valign="top">
<asp:Label ID="lblAddressStreet1" runat="server" Text='<%# Eval("Street1") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street1")) ? false : true %>' />
<asp:Label ID="lblAddressStreet2" runat="server" Text='<%# Eval("Street2") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street2")) ? false : true %>' />
<asp:Label ID="lblAddressStreet3" runat="server" Text='<%# Eval("Street3") + "<br />" %>' Visible='<%# Convert.IsDBNull(Eval("Street3")) ? false : true %>' />
<asp:Label ID="lblAddressCity" runat="server" Text='<%# Eval("City") %>' />, <asp:Label ID="lblAddressProvince" runat="server" Text='<%# Eval("Province") %>' /> <asp:Label ID="lblAddressPostalCode" runat="server" Text='<%# Eval("PostalCode") %>' /><br />
<asp:Label ID="lblAddressCountry" runat="server" Text='<%# Eval("Country") + "<br />" %>' Visible='<%# (Eval("Country", "{0}") == "Canada") ? false : true %>' />
<table border="0">
<asp:Label ID="lblAddressPhone" runat="server" Text='<%# "<tr><td width=50>Phone:</td><td>" + Eval("Phone") + "</td></tr>" %>' Visible='<%#Convert.IsDBNull(Eval("Phone")) ? false : true %>' />
<asp:Label ID="lblAddressFax" runat="server" Text='<%# "<tr><td width=50>Fax:</td><td>" + Eval("Fax") + "</td></tr>" %>' Visible='<%#Convert.IsDBNull(Eval("Fax")) ? false : true %>' />
<asp:PlaceHolder ID="cphEmailAddress" runat="server" Visible='<%# Convert.IsDBNull(Eval("Email")) ? false : true %>'><tr><td width="50">Email:</td><td><asp:LinkButton ID="lnkEmailValidate" runat="server" Text='<%# Eval("ShortEmail") %>' OnClick="ShowReCAPTCHA" /></td></tr></asp:PlaceHolder>
<asp:PlaceHolder ID="cphEmail" runat="server" Visible="false"><tr><td width="50">Email:</td><td><asp:HyperLink ID="lnkEmail" runat="server" NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' Text='<%# Eval("Email") %>' /></td></tr></asp:PlaceHolder>
</table>
<asp:PlaceHolder ID="cphCaptcha" runat="server" Visible="false">
<br />
<div class="info">To view the full email address, please type the moving characters in the box below.</div>
<nu:NuCaptchaControl ID="nucaptcha" runat="server" ClientKey="LEAP|0|4|TYPE|9|CLIENTKEY|CID|4|9942|KID|4|9884|SKEY|32|bDdiOWgwdjhtNy1MdTRENG43Q1BZdyws" />
</asp:PlaceHolder>
</td>
</tr>
</table>
<br />
</itemtemplate>
</asp:Repeater>
<br />
<br />
<asp:LinkButton ID="lnkDetailsBack" CssClass="navButtonLarge" runat="server" OnClick="lnkDetailsBack_Click">Back to Search Results</asp:LinkButton>
<asp:LinkButton ID="lnkDetailsNewSearch" runat="server" CssClass="navButtonLarge" OnClick="lnkNewSearch_Click">New Search</asp:LinkButton>
<asp:SqlDataSource ID="dsDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:MSCRM %>"
SelectCommand="
SELECT * from some table
">
<selectparameters>
<asp:Parameter DbType="Guid" Name="ContactID" />
</selectparameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsSpecialty" runat="server"
ConnectionString="<%$ ConnectionStrings:MSCRM %>"
SelectCommand="
SELECT * from some table
">
<selectparameters>
<asp:Parameter DbType="Guid" Name="ContactID" />
</selectparameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsAddresses" runat="server"
ConnectionString="<%$ ConnectionStrings:xxx %>"
SelectCommand="
SELECT * from some table
">
<selectparameters>
<asp:Parameter DbType="Guid" Name="ContactID" />
</selectparameters>
</asp:SqlDataSource>
</asp:Panel>
<!--//////////// Begin Data Sources ////////////-->
</asp:Content>
default.aspx.cs
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class directory_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack && pnlDetails.Visible == true && rpAddresses.Items.Count > 0 && ((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible == true)
{
Page.Validate();
if (Page.IsValid)
{
((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible = false;
((PlaceHolder)rpAddresses.Items[0].FindControl("cphEmailAddress")).Visible = false;
((PlaceHolder)rpAddresses.Items[0].FindControl("cphEmail")).Visible = true;
}
}
}
protected void rpAddresses_DataBound(object sender, EventArgs e)
{
}
protected void lnkSearch_Click(object sender, EventArgs e)
{
//lvResults.DataBind();
DataPager1.SetPageProperties(0, DataPager1.PageSize, true);
ShowResults();
}
protected void ShowResults()
{
pnlSearchbox.Visible = false;
pnlResults.Visible = true;
pnlDetails.Visible = false;
}
protected void lnkNewSearch_Click(object sender, EventArgs e)
{
pnlSearchbox.Visible = true;
pnlResults.Visible = false;
pnlDetails.Visible = false;
}
protected void lnkDetails_Click(object sender, CommandEventArgs e)
{
dsDetails.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();
dsSpecialty.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();
dsAddresses.SelectParameters["ContactID"].DefaultValue = e.CommandArgument.ToString();
rpAddresses.DataBind();
if (rpAddresses.Items.Count == 0)
{
rpAddresses.Visible = false;
}
else
{
rpAddresses.Visible = true;
}
pnlSearchbox.Visible = false;
pnlResults.Visible = false;
pnlDetails.Visible = true;
}
protected void lnkDetailsBack_Click(object sender, EventArgs e)
{
ShowResults();
}
protected void dsSearchResults_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
lblCount.Text = String.Format("{0} Dentists found", e.AffectedRows);
}
protected void ShowReCAPTCHA(object sender, EventArgs e)
{
((PlaceHolder)rpAddresses.Items[0].FindControl("cphCaptcha")).Visible = true;
}
}
Thanks for the answers people, I did try some of these things.
Every control which you wish to access in aspx.cs should have runat="server"
they do
Build your solution after adding the above tag
If you wish to access the control defined inside the item template of repeater you
can do so only inside repeater events for by putting a loop reading
all items of repeater
they are
Right click on the page where you are getting the error and click on
convert to web application... That will solve your error.
no such option in my IDE, besides I think it would be the whole project, not a page that I convert.
Check to see if this is a web application project or a website
project. A website project would use the CodeFile attribute of the
#Page directive, but a web application project wouldn't. If it's
trying to compile as a web application project, you would receive
these errors because a web application project expects a designer file
in addition to the .aspx and .aspx.cs files. The designer contains all
the control definitions which in turn which allows you to reference
them properly in codebehind (web application uses CodeBehind and not
CodeFile attribute as well).
apparently then my project is a website, not a web application, as it includes the codefile attribute. also, I don't have designer files, so this makes sense.
Turns out I am trying to edit a solution developed in VS2008, using VS2013, and a lot has changed since then. While the issue was not resolved here, the fix was to create a new VS2013 solution and import the files, it's all working as intended now, thank you.
exclude or delete any other pages that reference the same code-behind file, for example an older page that you copied and pasted.
You may want to check the build action of the c# files in the property window in visual studio and make sure its set to Compile
Check to see if this is a web application project or a website project. A website project would use the CodeFile attribute of the #Page directive, but a web application project wouldn't. If it's trying to compile as a web application project, you would receive these errors because a web application project expects a designer file in addition to the .aspx and .aspx.cs files. The designer contains all the control definitions which in turn which allows you to reference them properly in codebehind (web application uses CodeBehind and not CodeFile attribute as well).
Right click on the page where you are getting the error and click on convert to web application... That will solve your error.
Couple of things to be checked here:
1. Every control which you wish to access in aspx.cs should have runat="server"
2. Build your solution after adding the above tag
3. If you wish to access the control defined inside the item template of repeater you can do so only inside repeater events for by putting a loop reading all items of repeater
I had this error but had just made a silly mistake. I wanted to retain the original code while I made some big changes so I copied and pasted one of the .cs files. Well, both files had the same class name. The two identical namespaces (understandably) confused the compiler. Removing the "backup" fixed it. I could have just changed the namespace, too.
Just sharing in case someone is as bone-headed as I.

Can't delete record in formview when a value is NULL

I am having problems deleting a record in a FormView that is wired up to a LinqDataSource. The table the LinqDataSource is using allows NULL values for almost all fields. If I create a new record in the FormView and fill in all the values, then try to delete it it works fine. However, if I leave any of the fields blank then save and try to delete, it throws the following exception:
Failed to set one or more properties on type
PRIDE.Info___Employee_Health_History. is not a valid value for
Int16.
The same happens for the real columns in the table. If I leave one of them blank, then save and try to delete I get the same exception with a different type.
My biggest question is, why is it trying to set ANY values when deleting?
Here's my table:
My FormView code:
<asp:FormView ID="fvHealthHistory" runat="server" CssClass="full"
AllowPaging="True" DataSourceID="ldsHealthHistory" DataKeyNames="ID"
oniteminserting="fvHealthHistory_ItemInserting">
<EmptyDataTemplate>
<p>No records found.</p>
<p>
<asp:Button ID="btnNew" runat="server" Text="New Record" CommandName="New" />
</p>
</EmptyDataTemplate>
<InsertItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</p>
</InsertItemTemplate>
<EditItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</p>
</EditItemTemplate>
<ItemTemplate>
<table class="pad5">
<tr>
<td class="field-name">Date:</td>
<td>
<asp:Label ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
</td>
</tr>
<tr>
<td class="field-name">Weight:</td>
<td><asp:Label ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
</tr>
<tr>
<td class="field-name">Height:</td>
<td><asp:Label ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
</tr>
<tr>
<td class="field-name">Pulse:</td>
<td><asp:Label ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
</tr>
<tr>
<td class="field-name">Systolic:</td>
<td><asp:Label ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
</tr>
<tr>
<td class="field-name">Diastolic:</td>
<td><asp:Label ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
</tr>
</table>
<p>
<asp:Button ID="btnNew" runat="server" Text="New" CommandName="New" />
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</p>
</ItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast"
FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Previous" />
<PagerStyle CssClass="pager" />
</asp:FormView>
The LinqDataSoure's ItemInserting event:
protected void fvHealthHistory_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values["Worker_ID"] = Convert.ToInt32(Request.QueryString["id"]);
}
And finally my LinqDataSource:
<asp:LinqDataSource ID="ldsHealthHistory" runat="server"
ContextTypeName="PRIDE.PRIDEDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" EntityTypeName="" OrderBy="Date desc"
TableName="Info___Employee_Health_Histories" Where="Worker_ID == #Worker_ID">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="0" Name="Worker_ID"
QueryStringField="id" Type="Int32" />
</WhereParameters>
<InsertParameters>
<asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
</UpdateParameters>
</asp:LinqDataSource>
Does anybody know why it would only let me delete a record if ALL the nullable fields are filled in?

How to insert an image in this ListView that is used for inserting something else?

I am a new ASP.NET developer and I have to create a quiz engine application. I am using a ListView for inserting the questions under specific quiz.
I have the following database design:
QuizContent Table: ID, QuizID, QuestionID, AnswerID
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
Answers Table: AnswerID, Answer
QuestionImage: ID, QuestionID, URL
What I want now is making the admin being able to embedd an image to any one of these questions. How can I do that since I have two different tables in the database? One for the questions and one for the images as shown in the design above. I have this design because not all of the questions will have images. Besides that, I may have two or three questions or more having the same image.
I know I should not post too much code or information, but I have to do it to make it clear:
<div align="center">
<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2"
DataKeyNames="QuestionID" InsertItemPosition="LastItem" OnSelectedIndexChanged="ListView2_SelectedIndexChanged">
<EditItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" ToolTip="Update" runat="server" CommandName="Update" />
<asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
</td>
<td>
<asp:TextBox ID="QuestionTextBox" runat="server"
Text='<%# Bind("Question") %>' />
</td>
<td>
<asp:TextBox ID="QuestionOrderTextBox" runat="server"
Text='<%# Bind("QuestionOrder") %>' />
</td>
<td>
<asp:TextBox ID="AnswerExplanationTextBox" runat="server"
Text='<%# Bind("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:TextBox ID="UrlTextBox" runat="server" Text='<%# Bind("Url") %>' />
</td>--%>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="InsertButton" ImageUrl="Images/icons/add24.png" ToolTip="Add" runat="server" CommandName="Insert" />
<asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/clear24.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
</td>
<td>
<asp:TextBox ID="QuestionTextBox" runat="server"
Text='<%# Bind("Question") %>' />
</td>
<td>
<asp:TextBox ID="QuestionOrderTextBox" runat="server"
Text='<%# Bind("QuestionOrder") %>'/>
</td>
<td>
<asp:TextBox ID="AnswerExplanationTextBox" runat="server"
Text='<%# Bind("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:FileUpload ID="ImageIDUploader" runat="server" size="10" />
</td>--%>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="delete" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
<asp:ImageButton ID="SelectButton" ImageUrl="Images/icons/select.png" ToolTip="Select" runat="server" CommandName="Select" />
</td>
<td>
<asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
</td>
<td>
<asp:Label ID="QuestionOrderLabel" runat="server"
Text='<%# Eval("QuestionOrder") %>' />
</td>
<td>
<asp:Label ID="AnswerExplanationLabel" runat="server"
Text='<%# Eval("AnswerExplanation") %>' />
</td>
<%--<td>
<a href='<%# Eval("URL") %>' target="_blank">
<asp:Label ID="ImageURLlbl" runat="server" Text='<%# Eval("URL") %>' />
</a>
</td>--%>
</tr>
</ItemTemplate>
<LayoutTemplate>
<div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
<thead>
<tr style="background-color:#C6D7B5;">
<th style="border-bottom:2px solid #003366; ">...</th>
<th style="border-bottom:2px solid #003366; ">Question</th>
<th style="border-bottom:2px solid #003366; ">Question Order</th>
<th style="border-bottom:2px solid #003366; ">Answer Explanation</th>
<th style="border-bottom:2px solid #003366; ">Image (URL)</th>
</tr>
</thead>
<tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
</table></div>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="Delete" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
</td>
<td>
<asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
</td>
<td>
<asp:Label ID="QuestionOrderLabel" runat="server"
Text='<%# Eval("QuestionOrder") %>' />
</td>
<td>
<asp:Label ID="AnswerExplanationLabel" runat="server"
Text='<%# Eval("AnswerExplanation") %>' />
</td>
<%--<td>
<asp:Label ID="ImageURLlbl" runat="server"
Text='<%# Eval("URL") %>' />
</td>--%>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>"
SelectCommand="SELECT QuestionID, Question, QuestionOrder, AnswerExplanation
FROM dbo.Question
WHERE (QuestionID IN
(SELECT DISTINCT QuestionID
FROM dbo.QuizContent
WHERE (QuizID = #QuizID)))"
DeleteCommand="DELETE FROM [Question] WHERE [QuestionID] = #QuestionID"
InsertCommand="INSERT INTO [Question] ([Question], [QuestionOrder], [AnswerExplanation]) VALUES (#Question, #QuestionOrder, #AnswerExplanation)"
UpdateCommand="UPDATE [Question] SET [Question] = #Question, [QuestionOrder] = #QuestionOrder, [AnswerExplanation] = #AnswerExplanation WHERE [QuestionID] = #QuestionID">
<DeleteParameters>
<asp:Parameter Name="QuestionID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QuestionOrder" Type="Int32" />
<asp:Parameter Name="AnswerExplanation" Type="String" />
<asp:ControlParameter ControlID="ListView1" Name="QuizID" PropertyName="SelectedValue" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QuestionOrder" Type="Int32" />
<asp:Parameter Name="AnswerExplanation" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ListView1" Name="QuizID" DefaultValue="0"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
So how to do that?
Don't add the image in the same list view, but show a selection of existing images with an option to add another one that opens a popup with the list of all images and an option to upload a new one.
Create a view to combine questions and image:
CREATE VIEW QuestionsView AS
SELECT Question.*, QuestionImage.ID AS ImageID
FROM Question LEFT OUTER JOIN QuestionImage ON Question.QuestionID = QuestionImage.QuestionID
Use this view to select records for your ListView.
To show a DropDownList:
<td>
<asp:DropDownList runat="server" DataSourceID="imagesDataSource" SelectedValue="<%# Bind("ImageID") %>" DataTextField="URL" DataValueField="ID" />
<asp:SqlDataSource runat="server" ID="imagesDataSource" SelectCommand="SELECT * FROM QuestionImage" />
</td>
Change your ListView data source to use a stored procedure for update that gets a parameter for each Bind use.
Add a link to open the images list popup. For the popup you can use one of the samples here: http://smashingspy.com/32-best-jquery-popup-window-dialog-box-example/

Object reference not set to an instance of an object errors when using CreateUserWizard [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Object reference not set to an instance of an object errors when using CreateUserWizard
The error - "Object reference not set to an instance of an object" occured when I hit the "Next' button of my CreateUserWizard controls. I had checked through my codes that I did not leave any of my variables null. but i still can't solve this errors.
This is the caused of the error:
Line 31: DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
Source Error:
Line 29: object UserGUID = User.ProviderUserKey;
Line 30:
Line 31: DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
Line 32:
Line 33: DataSource.Insert();
This is my html code file:
<form id="form1" runat="server">
<div>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
OnCreatedUser="CreateUserWizard1_CreatedUser" CreateUserButtonText="Next"
FinishCompleteButtonText="Create User" Height="330px"
Width="512px">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep2" runat="server" >
<ContentTemplate>
<table>
<tr>
<th>User Information</th>
</tr>
<tr>
<td>Username:</td>
<td class="style2">
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator9" ControlToValidate="UserName"
ErrorMessage="Username is required." />
</td>
</tr>
<tr>
<td>Password:</td>
<td class="style2">
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator10" ControlToValidate="Password"
ErrorMessage="Password is required." />
</td>
</tr>
<tr>
<td>Confirm Password:</td>
<td class="style2">
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator13" ControlToValidate="ConfirmPassword"
ErrorMessage="Confirm Password is required." />
</td>
</tr>
<tr>
<td>
Email:</td>
<td class="style2">
<asp:TextBox runat="server" ID="Email" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator16" runat="server" ControlToValidate="Email"
ErrorMessage="Email required."></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Question:</td>
<td class="style2">
<asp:TextBox runat="server" ID="Question" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator12" ControlToValidate="Question"
ErrorMessage="Question is required." />
</td>
</tr>
<tr>
<td>Answer:</td>
<td class="style2">
<asp:TextBox runat="server" ID="Answer" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator14" ControlToValidate="Answer"
ErrorMessage="Answer is required." />
</td>
</tr>
<tr>
<td colspan="2">
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:WizardStep ID="CreateUserWizardStep0" runat="server" Title="User Details">
<table>
<tr>
<th>Billing Information</th>
</tr>
<tr>
<td>Name:</td>
<td class="style1">
<asp:TextBox runat="server" ID="CustName" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator18" ControlToValidate="CustName"
ErrorMessage="Name is required." />
</td>
</tr>
<tr>
<td>Contact Number:</td>
<td class="style1">
<asp:TextBox runat="server" ID="CustNum" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator19" ControlToValidate="CustNum"
ErrorMessage="Contact num is required." />
</td>
</tr>
<tr>
<td>Role:</td>
<td class="style1">
<asp:DropDownList ID="CustRole" runat="server" AutoPostBack="True" Height="16px"
Width="123px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Bride</asp:ListItem>
<asp:ListItem>Groom</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator20" ControlToValidate="CustRole"
ErrorMessage="Role is required." />
</td>
</tr>
<tr>
<td>
Status:</td>
<td class="style1">
<asp:DropDownList ID="CustStatus" runat="server" AutoPostBack="True"
Height="17px" Width="121px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Status A</asp:ListItem>
<asp:ListItem>Status B</asp:ListItem>
<asp:ListItem>Status C</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" ControlToValidate="CustStatus"
ErrorMessage="Status is required."/>
</td>
</tr>
<tr>
<td>
</td>
<td class="style1">
</td>
</tr>
<tr>
<td>
Partner's Name:</td>
<td class="style1">
<asp:TextBox runat="server" ID="PName" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator5" ControlToValidate="PName"
ErrorMessage="PName is required." />
</td>
</tr>
<tr>
<td>
Partner Email:</td>
<td class="style1">
<asp:TextBox runat="server" ID="PEmail" />
</td>
</tr>
<tr>
<td>
PRole:
</td>
<td class="style1">
<asp:DropDownList ID="PRole" runat="server" AutoPostBack="True" Height="16px"
Width="123px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Bride</asp:ListItem>
<asp:ListItem>Groom</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator7" ControlToValidate="PRole"
ErrorMessage="PRole is required." />
</td>
</tr>
<tr>
<td>
Wed Date:
</td>
<td class="style1">
<cc1:DatePicker ID="WedDate" runat="server" CalendarDate=""
TextCssClass="" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ControlToValidate="WedDate"
ErrorMessage="Wedding date required"/>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
<ContentTemplate>
<table>
<tr>
<td align="center" colspan="2">
Complete</td>
</tr>
<tr>
<td>
Your account has been successfully created.</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="ContinueButton" runat="server" CausesValidation="False"
CommandName="Continue" Text="Continue" ValidationGroup="CreateUserWizard1" />
</td>
</tr>
</table>
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"
InsertCommand="INSERT INTO [UserDetails] ([UserId], [CustName], [CustNum], [CustRole], [CustStatus], [PName], [PEmail], [PRole], [WedDate]) VALUES (#UserId, #CustName, #CustNum, #CustRole, #CustStatus, #PName, #PEmail, #PRole, #WedDate)"
ProviderName="<%$ ConnectionStrings:ASPNETDBConnectionString1.ProviderName %>">
<InsertParameters>
<asp:ControlParameter Name="CustName" Type="String" ControlID="CustName" PropertyName="Text" />
<asp:ControlParameter Name="CustNum" Type="String" ControlID="CustNum" PropertyName="Text" />
<asp:ControlParameter Name="CustRole" Type="String" ControlID="CustRole" PropertyName="Text" />
<asp:ControlParameter Name="CustStatus" Type="String" ControlID="CustStatus" PropertyName="Text" />
<asp:ControlParameter Name="PName" Type="String" ControlID="PName" PropertyName="Text" />
<asp:ControlParameter Name="PEmail" Type="String" ControlID="PEmail" PropertyName="Text" />
<asp:ControlParameter Name="PRole" Type="String" ControlID="PRole" PropertyName="Text" />
<asp:ControlParameter Name="WedDate" Type="String" ControlID="WedDate" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
<br />
</div>
This is the code behind:
using System;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
TextBox UserName=
(TextBox)CreateUserWizardStep2.ContentTemplateContainer.FindControl("UserName");
SqlDataSource DataSource =
(SqlDataSource)CreateUserWizardStep2.ContentTemplateContainer.FindControl("InsertExtraInfo");
MembershipUser User = Membership.GetUser(UserName.Text);
object UserGUID = User.ProviderUserKey;
DataSource.InsertParameters.Add("UserId", UserGUID.ToString()); //the error lies at here
DataSource.Insert();
}
}
The InsertExtraInfo is actually inside CompleteWizardStep1. Try this:
SqlDataSource DataSource =
(SqlDataSource)CompleteWizardSetp1.ContentTemplateContainer
.FindControl("InsertExtraInfo");
But I believe you can use the outermost container CreateUserWizard1 so you don't have to worry about which step it's in.
CreateUserWizard1.FindControl("InsertExtraInfo");
you dont need to place SqlDataSource control inside wizardcontrol. move it out. and you dont need to find it. just access it by its ID.

How to make this DropDownList works inside the ListView?

I have two tables in my database as following:
Employee Table: Username, Name, Job, DivisonCode
Division Table: DivisionCode, DivisionName
I am using ListView to show the information of the employee. Instead of showing the DivisionCode, I put a DropDownList that for showing the DivsionNames and of course I put this DropDownList inside the EditItemTemplate in the ListView. Everything works fine except editing the employee information. When I tried to change the division of the employee, I got the following error:
Cannot insert the value NULL into column 'DivisionCode', table
'psspdbTest.dbo.employee'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
My Code in ASP.NET:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Username"
DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel1" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="DivisionName"
DataValueField="SapCode">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
DeleteCommand="DELETE FROM [Divisions] WHERE [SapCode] = #SapCode"
InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionName]) VALUES (#SapCode, #DivisionName)"
SelectCommand="SELECT * FROM [Divisions]"
UpdateCommand="UPDATE [Divisions] SET [DivisionName] = #DivisionName WHERE [SapCode] = #SapCode">
<DeleteParameters>
<asp:Parameter Name="SapCode" Type="Double" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SapCode" Type="Double" />
<asp:Parameter Name="DivisionName" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DivisionName" Type="String" />
<asp:Parameter Name="SapCode" Type="Double" />
</UpdateParameters>
</asp:SqlDataSource>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
</td>
<td>
<asp:TextBox ID="UsernameTextBox" runat="server"
Text='<%# Bind("Username") %>' />
</td>
<td>
<asp:TextBox ID="JobTitleTextBox" runat="server"
Text='<%# Bind("JobTitle") %>' />
</td>
<td>
<asp:TextBox ID="BadgeNoTextBox" runat="server" Text='<%# Bind("BadgeNo") %>' />
</td>
<td>
<asp:TextBox ID="EmpOrgTypeTextBox" runat="server"
Text='<%# Bind("EmpOrgType") %>' />
</td>
<td>
<asp:TextBox ID="DivisionCodeTextBox" runat="server"
Text='<%# Bind("DivisionCode") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit"
Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server" Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0"
style="">
<tr runat="server" style="">
<th runat="server">
</th>
<th runat="server">
Name</th>
<th runat="server">
Username</th>
<th runat="server">
JobTitle</th>
<th runat="server">
BadgeNo</th>
<th runat="server">
EmpOrgType</th>
<th runat="server">
DivisionCode</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
</td>
<td>
<asp:Label ID="JobTitleLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
<td>
<asp:Label ID="BadgeNoLabel" runat="server"
Text='<%# Eval("BadgeNo") %>' />
</td>
<td>
<asp:Label ID="EmpOrgTypeLabel" runat="server"
Text='<%# Eval("EmpOrgType") %>' />
</td>
<td>
<asp:Label ID="DivisionCodeLabel" runat="server"
Text='<%# Eval("DivisionCode") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.employee.Name, dbo.employee.Username, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.EmpOrgType, dbo.employee.DivisionCode
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = #Username"
InsertCommand="INSERT INTO [employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (#Name, #Username, #JobTitle, #BadgeNo, #EmpOrgType, #DivisionCode)"
UpdateCommand="UPDATE [employee] SET [Name] = #Name, [JobTitle] = #JobTitle, [BadgeNo] = #BadgeNo, [EmpOrgType] = #EmpOrgType, [DivisionCode] = #DivisionCode WHERE [Username] = #Username">
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Username" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="Double" />
<asp:Parameter Name="EmpOrgType" Type="Double" />
<asp:Parameter Name="DivisionCode" Type="Double" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
So how to solve this problem?
Add this to your DropDownList SelectedValue='<%# Bind("DivisionCode") %>'
and remove AutoPostBack="true"

Categories

Resources