Solved

dbnull in webform

Posted on 2013-01-07
17
254 Views
Last Modified: 2013-01-08
I have a web form where in the grid template I am creating a button and passing a parameter to C# if they add a new line the parameter is not yet created and I am getting an error "Object cannot be cast from DBNull to other types."
The code is
<EditItemTemplate>
                                        <label>Documents: </label>
                                           <%# AllowUploadDocument(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "documentid")))%> <-- error on documentid
                                        </EditItemTemplate>

Open in new window


can anyone help - how do I get it to pass a 0 or something so I can tell it in C# to ignore it
0
Comment
Question by:r3nder
  • 12
  • 5
17 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38752060
Try:

<%# AllowUploadDocument(Convert.ToInt32(Container.DataItem is DBNull ? 0 : DataBinder.Eval(Container.DataItem, "documentid")))%>

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38752142
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'
<%#  AllowUploadDocument(Convert.ToInt32(Container.DataItem is DBNull ? 0 : "podocumentuid"))%>

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38752149
Input string was not in a correct format.
<%#  AllowUploadDocument(Convert.ToInt32(Container.DataItem is DBNull ? "0" : "podocumentuid"))%>

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752250
<%# AllowUploadDocument(Convert.ToInt32(Container.DataItem is DBNull ? "0" : DataBinder.Eval(Container.DataItem, "documentid")))%>

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38752274
Object cannot be cast from DBNull to other types.
0
 
LVL 6

Author Comment

by:r3nder
ID: 38752289
it work as long  as I dont try to add a new document - if I open an existing document it goes through just fine
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752313
Hmmm...  Can you try:

<%# AllowUploadDocument(Convert.ToInt32(Container.DataItem == DBNull.Value ? "0" : DataBinder.Eval(Container.DataItem, "documentid")))%>

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38752345
Object cannot be cast from DBNull to other types.

i also tried
<%# AllowUploadDocument(Convert.ToInt32(Container.DataItem == DBNull.Value ? 0 : DataBinder.Eval(Container.DataItem, "documentid")))%>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Author Comment

by:r3nder
ID: 38752419
I just dont get it - Its only the first half of the condition that has a problem - can we do it a different wa?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752433
It's been a while since I've done data binding in this fashion. If no one sorts you out before I get home this afternoon, I'll try to set up a test project and play around with.
0
 
LVL 6

Author Comment

by:r3nder
ID: 38752468
thanks Kaufmed - I still work on it but I  relax whebn I see you have responded :)
R
0
 
LVL 6

Author Comment

by:r3nder
ID: 38752552
can it be done in the JavaScript
like here:
Response.Redirect("AddDocumentPOL.aspx?action=add&podocumentuid=" + e.Item.OwnerTableView.ParentItem.GetDataKeyValue("podocumentuid").ToString());
0
 
LVL 6

Author Comment

by:r3nder
ID: 38752556
sorry c#
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752612
Where would you expect that bit of code to go?
0
 
LVL 6

Author Comment

by:r3nder
ID: 38752633
I was trying to look for where is is passing the documentid and change the value there but that wouldnt work -
I am passing it in an edit template in a grid template column using C# to return a string to build the button
 protected string AllowUploadDocument(int documentid)
        {
 
            string retStr;
            retStr = "";
            string sqlStr = "Select id, File_Name, File_ID, Extension FROM PODocument_Upload Where PONum= '" + documentid + "'";
            String ConnString = WebConfigurationManager.ConnectionStrings["WebConnectionString"].ToString();
            using(SqlConnection conn = new SqlConnection(ConnString))
            using(SqlCommand cmd = new SqlCommand(sqlStr, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection.Open();
                object count = cmd.ExecuteScalar();
                int i = Convert.ToInt32(count);

                if (i >= 1)
                {
                    retStr = @" <input type=""image"" src= ""image/documents-blue.gif"" value=""Add a Document"" name=""RadGrid1$ctl00$ctl10$btnOpen"" id=""RadGrid1_ctl00_ctl10_btnOpen"" OnClick=""ShowExisting2('Docs'," + documentid + @"); return false;"" />";
                    return retStr;
                }
                
                else
                {
                    retStr = @"<input type=""image"" src= ""image/documents-ltpink.gif"" value=""Add a Document"" name=""RadGrid1$ctl00$ctl10$btnOpen"" id=""RadGrid1_ctl00_ctl10_btnOpen"" OnClick=""ShowExisting2('Docs'," + documentid + @"); return false;"" />";
                    return retStr;
                }
            }
            
        }

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38752814
GOT IT
<%# AllowUploadDocument(DataBinder.Eval(Container.DataItem,"documentid") == DBNull.Value ? 0 : Convert.ToInt32(DataBinder.Eval(Container.DataItem, "documentid")))%>

thanks for the help Kaufmed
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38755625
If you hadnt given me the idea for using a switch I would never have gotten it
Thanks Kaufmed -- as always :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

13 Experts available now in Live!

Get 1:1 Help Now