[Solved] ASP.Net Error: Could not convert object from DBNull to other types

kankon
 
on Jul 12, 2021 11:11 PM
903 Views

hello,

when i try sesrch  users data it showe all info

but when i try add convert for date its not show image profile

Error Could not convert object from DBNull to other types.

        protected void Searchdata()
        {
            string dtVal = workdate.Text.Trim();
            DateTime Dob = Convert.ToDateTime(dtVal);
            Label4.Text = CalculateAge(Dob);
            try
            {
                String constring = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
                SqlConnection sqlcon = new SqlConnection(constring);

                String pname = "Insert_Userupdate"; ;

                sqlcon.Open();
                SqlCommand com = new SqlCommand(pname, sqlcon);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("civilid", txtSearch.Text);
                com.Parameters.AddWithValue("name", txtSearch.Text);
                SqlDataReader rdr;
                rdr = com.ExecuteReader();
                if (rdr.Read())
                {                   
                    txtUsername.Text = rdr["name"].ToString();
                    joptitel.Text = rdr["jop"].ToString();
                    markazel3mel.Text = rdr["markazel3mal"].ToString();
                    eldraja.Text = rdr["eldarajah"].ToString();
                    filenumber.Text = rdr["fileid"].ToString();
                    civilid.Text = rdr["civilid"].ToString();
                    mobile.Text = rdr["mobile"].ToString();
                    workdate.Text = rdr["workdate"].ToString();
                    sptotal.Text = rdr["SpecialTotalPoints"].ToString();
                    spmonth.Text = rdr["SpecialMonthPoints"].ToString();
                    rsmtotal.Text = rdr["rasmeTotalPoints"].ToString();
                    rsmonth.Text = rdr["rasmeMonthPoints"].ToString();
                    EMEmonth.Text = rdr["EmergencyMonthPoints"].ToString();
                    EMEtotal.Text = rdr["EmergencyTotalPoints"].ToString();
                    txtPassword.Text = rdr["Password"].ToString();
                    groupENG0.Text = rdr["blockspecial"].ToString();
                    groupENG2.Text = rdr["blockrasme"].ToString();
                    group1.Text = rdr["OPkdamat"].ToString();
                    groupsection.Text = rdr["OPnqlyat"].ToString();
                    groupMrkzel3ml.Text = rdr["OPusers"].ToString();
                    groupENG.Text = rdr["OPsec"].ToString();
                    txtEmail.Text = rdr["Email"].ToString();
                    Adminset.Text = rdr["RoleId"].ToString();                   
                    date1.Text = Convert.ToDateTime(rdr["DateTimePicker1"]).ToString("yyyy/MM/dd");
                    date2.Text = Convert.ToDateTime(rdr["DateTimePicker2"]).ToString("yyyy/MM/dd");

                    byte[] bytes = null;
                    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                    System.Drawing.Image img = (System.Drawing.Image)converter.ConvertFrom((byte[])rdr["profileimage"]);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        bytes = ms.ToArray();
                    }
                    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                    im2.ImageUrl = "data:image/png;base64," + base64String;                   
                }
                else
                {
                    txtUsername.Text = "";

                    civilid.Text = "";

                     Adminset.Text = "";
                     im2.ImageUrl = "";

                    ClientScript.RegisterClientScriptBlock(GetType(), "alert", "alert('" + "لاتوجد بيانات\\n " + txtSearch.Text.ToString() + "');", true);
                }

                sqlcon.Close();
            }
            catch (Exception)
            {
            }
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 13, 2021 07:52 AM
kankon says:
                    byte[] bytes = null;
                    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                    System.Drawing.Image img = (System.Drawing.Image)converter.ConvertFrom((byte[])rdr["profileimage"]);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        bytes = ms.ToArray();
                    }
                    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                    im2.ImageUrl = "data:image/png;base64," + base64String;

Change with below.

if(!Convert.IsDBNull(rdr["profileimage"]))
{
    byte[] bytes = null;
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    System.Drawing.Image img = (System.Drawing.Image)converter.ConvertFrom((byte[])rdr["profileimage"]);
    using (MemoryStream ms = new MemoryStream())
    {
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        bytes = ms.ToArray();
    }
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    im2.ImageUrl = "data:image/png;base64," + base64String;
}