Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-28
7
Medium Priority
?
496 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

927 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