I am converting image from string by FromBase64String in asp.net C#, but at that time it showing title error in web method.
Here is my code,
[WebMethod]
public static List<CustomerMortgageModel> GetProductList()
{
string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
List<CustomerMortgageModel> customers = new List<CustomerMortgageModel>();
Service service = new Service();
using (SqlConnection con = new SqlConnection(constr))
{
string qrySelProductDetail = "select * from tbl_MortageDetail " + System.Environment.NewLine;
using (SqlCommand cmd = new SqlCommand(qrySelProductDetail, con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
byte[] bytes = Convert.FromBase64String(sdr["DesignImage"].ToString());
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
}
customers.Add(new CustomerMortgageModel
{
DesignImage = image.ToString()
});
}
}
con.Close();
}
}
return customers;
}
The error coming from this line
byte[] bytes = Convert.FromBase64String(sdr["DesignImage"].ToString());
use
so you'll be sure of the column's type and that the string isn't empty, if you still get an error at 'Convert.FromBase64String' it's impossible to find the problem without having a sample of the invalid base64 content (but paste it here only if it doesn't contain private data)