Sunday, 18 September 2016

Show list of items with searchable keyword at runtime (as a dropdown list) using textbox with the help of AjaxTool in Asp.net C#

STEP 1:

   First of all add ajaxTool element in codebehind of program.

Take a textbox as shown below and then add the AjaxToolKit element with this textbox.


<asp:TextBox ID="txtCatgrySerch" runat="server" OnTextChanged="txtCatgrySerch_TextChanged"></asp:TextBox>

<ajaxToolkit:AutoCompleteExtender runat="server" ID="AutoCompleteExtender1" TargetControlID="txtCatgrySerch"
MinimumPrefixLength="1" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
FirstRowSelected="false" ServiceMethod="GetName" />


Instruction To Use:

 Write the TextBox id in TargetControlID and the Method Name That showing Below in ServiceMethod as shown above.

STEP 2:

public partial class Form_XYZ : System.Web.UI.Page
{
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetName(string prefixText)
{
dbConnection cn = new dbConnection();
cn.CreateObject();
using (MySqlDataAdapter adp = new MySqlDataAdapter("ProcedureName", ConnectionString))
{
List<string> E_NAME2 = new List<string>();
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
adp.SelectCommand.Parameters.AddWithValue("@SearchItem", prefixText);
DataTable dt = new DataTable();
adp.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
E_NAME2.Add(dt.Rows[i][0].ToString());
}
return E_NAME2;
}
}
}


Instruction To Use:
a)      Make Procedure for searching records from specific table on any condition.
b)      In this method Typed keyword will automatically pass in this method and will show the result.
c)       Important Note: Inside this method any server control will not work, you will unable to see the controls. In this case you can add HttpContext.Current.Session["AnyName"] for maintaing Session or anything else.

RESULT :


Wednesday, 10 August 2016

Convert Image to byte array and byte to image in ASP.NET C#

Convert Image to byte and then display in Asp.Net C#:

Suppose you are facing problem in showing image from folder to asp.net Image control. Then use below code to display image instant.

Reason behind using this code:

Suppose you have images in folder and path in database. then just pass path in this code and it will get image from folder using this path and then first it will convert into byte and then will directly assign to display to the image control

e.g:

System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(@"C:/FolderName/abc.jpg"));
                            byte[] bytes;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                bytes = ms.ToArray();                            
                            }
                            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                            Image1.ImageUrl = "data:image/png;base64," + base64String;
                            img.Dispose();


Convert image from Fileupload to byte and then display in ASP.Net C#

Reason behind using this code:

Suppose you want to upload new image to replace existing image then due to postback effect system does not show the new
image. In that case below code will first convert image into byte then assign to imageurl

e.g:

string imgType = System.IO.Path.GetExtension(FUploadImg.FileName);
                    int imgSize = FUploadImg.PostedFile.ContentLength;
                 
                    byte[] imgbyte = new byte[imgSize];
                    HttpPostedFile img = FUploadImg.PostedFile;
                    img.InputStream.Read(imgbyte, 0, imgSize);

string base64String = Convert.ToBase64String(imgbyte, 0, imgbyte.Length);
Image2.ImageUrl = "data:image/png;base64," + base64String;