Solved

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

Posted on 2010-11-28
7
486 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 46
FInd Image Control Gridview 3 37
Generate Unique ID in VB.NET 21 104
disable data migrations in visual studio 2017 4 50
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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