Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:-object cannot be cast from dbnull to other types in c# net
Error Line:-st.Append("
C#
foreach (DataRow item in dt.Rows)
        {
            // while (dr1.Read())
            foreach (string s in List)
            {
                // var arr = s;

                if (s != "S-1")
                {

                    if (s == item["locationNo"])
                    {
                        st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                }
                    else
                    {
                        st.Append("");
                        st.Append("<tr>");
                        st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />
                    }
                }
            }
        st.Append("</tr>");
    }
}
dr.Close(); 
st.Append("");
Literal1.Text = st.ToString();
Posted
Updated 12-Mar-15 22:19pm
v2
Comments
What is dbnull here? Debug and see.

1 solution

Try changing
if (s == item["locationNo"])

to
if (!DBNull.Value.Equals(item["locationNo"]) && s == (string)item["locationNo"])

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900