Solved

dbnull in webform

Posted on 2013-01-07
17
261 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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