Solved

FormView - Too many parameters - don't want one of multiple DataKeyNames

Posted on 2010-11-28
7
485 Views
Last Modified: 2012-05-10
Hi I have a FormView which allows editing but on updating I checked the update parameters and although I didn't specify 'LoadID' as a parameter, it is being included (because it is a DataKeyName) and causing an error.  Is there an easy way to stop LoadID being added to the parameter list?

<asp:FormView ID="fvCostJob" runat="server" CellPadding="4" DefaultMode="ReadOnly" DataKeyNames="LoadID,CostID" DataSourceID="SqlDataSource1" ForeColor="#333333">
0
Comment
Question by:Lorna70
  • 3
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34226787
Why is it a DataKeyName if it isn't actually a key field?
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34227141
You can specify the Update statement in the FormView's data source control (SqlDataSource). It should look something like this:

UPDATE table SET col1 = @col1parameter, col2 = @col2parameter WHERE LoadID = @LoadID

In this case, your update will not attempt to modify the value of the LoadID column (or any other columns not explicitly specified in the statement).
0
 

Author Comment

by:Lorna70
ID: 34229145
Doh - yes you're right, don't actually need LoadID!  As for SQL, I need to have everything as stored procedures for security.
Have discovered by looking at parameter names on updating method that the problem lies in the following line:

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

The value of SalesNotes in the database is Null and if I don't enter anything into this field in the form I get an error presumably because it's still null!  Is there a way of handling this in the form?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:AngryBinary
ID: 34232899
Having a null database value is a valid scenario, and shouldn't cause an exception unless you are attempting to access properties of that value without null checking first. What is the actual error you receive, and what code throws the error?
0
 

Author Comment

by:Lorna70
ID: 34233248
The code is:

foreach (DbParameter cp in CmdParams)
        {
            string pName = cp.ParameterName;
            string v = cp.Value == DBNull.Value ? "(null)" : cp.Value.ToString();

and if I check the parameter name it is the 'txtSalesNotes'.

The error is at the line 'string v = cp.Value...
The error is: NullReferenceException was unhandled by user code. Object reference not set to an instance of an object.  It works fine if I type some chars into the text box for txtSalesNotes so I need some way of being able to update the database table with that parameter even though it is null.
0
 
LVL 13

Accepted Solution

by:
AngryBinary earned 250 total points
ID: 34233627
I'll wager the actual problem is with "cp.Value.ToString()".

Try this:

string v = (DBNull.Value.Equals(cp.Value) || cp.Value == null) ? "(null)" : cp.Value.ToString();

If the value for the parameter is coming from user input, and not from the database, it wouldn't be a DBNull; it would be a regular old null, which is why your DBNull check wasn't catching it (DBNull.Value is not the same as "null").
0
 

Author Closing Comment

by:Lorna70
ID: 34233862
Yippee - it worked - thanks Angry Binary :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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