Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, September 24, 2020

A generic error occurred in GDI+ while creating image from Base64 string

In a web service, while converting a Base64 string to file, it is sometimes giving error  - "A generic error occurred in GDI+ while creating image from Base64 string".

Solution:

I have used the below code and problem resolved.


[HttpPost]
public ActionResult UploadSignatureTwo(String imageString){     
    var bytes = Convert.FromBase64String(imageString);
    using (var imageFile = new FileStream(Path.Combine(path,"test.jpeg"), FileMode.Create))
    {
        imageFile.Write(bytes, 0, bytes.Length);
        imageFile.Flush();
    }
}

Wednesday, January 15, 2020

System.InvalidCastException: 'Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'

System.InvalidCastException: 'Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'

 The error because the URL is not proper. please pass the proper url containing http:// or https://.

and also check that URL is correct.

Tuesday, October 22, 2019

file upload not working in updatepanel asp.net


The Fileupload Control is not working in the UpdatePanel in asp.net. To use the FileUpload control in the Updatepanel. We need to add the Post back trigger to the UpdatePanel Like the below Code

<asp:UpdatePanel ID="UpnlFileUpload" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FlUpload" runat="server" />
        <asp:Button ID="btnFileUpload" runat="server" Text="Upload" OnClick="btnFileUpload_OnClick" /> </ContentTemplate>
    <Triggers> <asp:PostBackTrigger ControlID="btnFileUpload" />
    </Triggers>
</asp:UpdatePanel>
Here we need to pass the Button Control ID in the PostBackTrigger Like the above code.

Generally after adding the PostBackTrigger it works accurate but some time it will not work. In this case you need to add the below code in the Page_Load .

Page.Form.Attributes.Add("enctype", "multipart/form-data");


Saturday, December 15, 2018

C# - Copy DataTable from one DataSet to another

When we trying to add a DataTable (that is inside of a different DataSet) to new DataSet ds. If I add it directly, we will get the following error:

                   DataTable already belongs to another DataSet.

DataTable.Copy

use DataTable.Copy to create a copy of your data table; then insert the copy into the target DataSet:

dataSetX.Tables.Add( dataTableFromDataSetY.Table[0].Copy() );

DataSet.Merge

You could also use DataSet.Merge for this:

dataSetX.Merge(dataTableFromDataSetY); Note, however, that if you are going to use this method, you might want to make sure that your target DataSet doesn't already contain a table with the same name:

If the target DataSet doesn't contain a table by the same name, a fresh copy of the table is created inside the data set;

If a table by the same name is already in the target data set, then it will get merged with the one passed to Merge, and you end up with a mix of the two.

Thursday, December 28, 2017

Upload Multiple Files Using FileUpload Control In ASP.NET 4.5 +

ASP.NET 4.5 and upper versions support this feature.

Now, FileUpload  control is built to support HTML 5, therefore it is only supported in browsers supporting HTML5. For other browsers it will work like a normal file upload control in ASP.NET.

A new Attribute introduces with the ASP.NET 4.5 FileUplaod control i.e. AllowMultipleIt takes either true or false.

In order to support multiple file uploads, set AllowMulitple="true" other than false.

<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />

In addition to this control, some new properties have been included to support code-behind coding.


HasFilesCheck whether FileUpload control has multiple files or not.

PostedFilesBasically used to get all the files from the FileUpload control in the iteration.

Example code to upload files:

   <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />  
   <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />  

protected void uploadFile_Click(object sender, EventArgs e) {  
    if (UploadImages.HasFiles) {  
        foreach(HttpPostedFile uploadedFile in UploadImages.PostedFiles) {  
            //code to save files  
        }  
    }  
} 



Friday, December 15, 2017

Textboxes in Modalpopup Panel adding commas on postback

The solution for this problem is to get the modalpoup out of the main update panel.

Once do this the commas will go away.

If you truly need an update panel inside of the modal popup, create one of it's own inside that panel.

Wednesday, November 29, 2017

What is the difference between smalldatetime and datetime?



DateTime SmallDateTime
Storage Size 8 Bytes 4 Bytes
FORMAT YYYY-MM-DD hh:mm:ss.nnn YYYY-MM-DD hh:mm:ss
MIN Value 1753-01-01 00:00:00 1900-01-01 00:00:00
MAX Value 9999-12-31 23:59:59.997 2079-06-06 23:59:00
Accuracy Rounded to increments of .000, .003, or .007 second
It means:
If time part in the date is 23:59:58.990 or 23:59:58.991, it will be stored as 23:59:58.990.
1 Minute
Second’s values that are 29.998 seconds or less are rounded down to the nearest minute. And second’s values of 29.999 seconds or more are rounded up to the nearest minute. So seconds part value is always 00.

Tuesday, November 28, 2017

C#, create folder dynamically or check folder exists

string subPath ="ImagesPath"; // your code goes here
bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!IsExi System.IO.Directory.CreateDirectory sts)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));

Monday, July 13, 2015

C#, Read a file

C#, Code to read a file 

String strHtmlMessage = String.Empty;
String strFilePath = HttpContext.Current.Server.MapPath("~/Mails/Confirmationmaill.htm");
  using (StreamReader sr = new StreamReader(strFilePath))
  {
    strHtmlMessage = sr.ReadToEnd();
 }

Thursday, December 24, 2009

Ajax CalendraExtender Hides Behind other controls

We can solve the problem of hiding CalenderExtender by increasing the z-index

Example:-

Add Javascript to the CalendraExtender event OnClientShownLike OnClientShown="onCalenderShown"

Javascript function:-

function onCalenderShown(sender,args)
{
sender._popupBehavior._element.style.zIndex=150002;
}