Solved

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

Posted on 2010-11-28
7
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

734 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