Solved

dbnull in webform

Posted on 2013-01-07
17
258 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
[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
  • 12
  • 5
17 Comments
 
LVL 75

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 75

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 75

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

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 75

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

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