Solved

dbnull in webform

Posted on 2013-01-07
17
255 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 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

15 Experts available now in Live!

Get 1:1 Help Now