Solved

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

Posted on 2010-11-28
7
477 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now