• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

dbnull in webform

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
r3nder
Asked:
r3nder
  • 12
  • 5
1 Solution
 
käµfm³d 👽Commented:
Try:

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

Open in new window

0
 
r3nderAuthor Commented:
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
 
r3nderAuthor Commented:
Input string was not in a correct format.
<%#  AllowUploadDocument(Convert.ToInt32(Container.DataItem is DBNull ? "0" : "podocumentuid"))%>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Open in new window

0
 
r3nderAuthor Commented:
Object cannot be cast from DBNull to other types.
0
 
r3nderAuthor Commented:
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
 
käµfm³d 👽Commented:
Hmmm...  Can you try:

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

Open in new window

0
 
r3nderAuthor Commented:
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
 
r3nderAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
r3nderAuthor Commented:
thanks Kaufmed - I still work on it but I  relax whebn I see you have responded :)
R
0
 
r3nderAuthor Commented:
can it be done in the JavaScript
like here:
Response.Redirect("AddDocumentPOL.aspx?action=add&podocumentuid=" + e.Item.OwnerTableView.ParentItem.GetDataKeyValue("podocumentuid").ToString());
0
 
r3nderAuthor Commented:
sorry c#
0
 
käµfm³d 👽Commented:
Where would you expect that bit of code to go?
0
 
r3nderAuthor Commented:
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
 
r3nderAuthor Commented:
GOT IT
<%# AllowUploadDocument(DataBinder.Eval(Container.DataItem,"documentid") == DBNull.Value ? 0 : Convert.ToInt32(DataBinder.Eval(Container.DataItem, "documentid")))%>

thanks for the help Kaufmed
0
 
r3nderAuthor Commented:
If you hadnt given me the idea for using a switch I would never have gotten it
Thanks Kaufmed -- as always :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 12
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now