Posts

Showing posts with the label C#

Error serializing - Exception of type System.Web.HttpUnhandledException was thrown.

Solved: You have to add [Serializable] attribute in the class and also add inherited class if you extend on that class. Why? Because you have created List or Dictionary type of property Inner Exception: System.ArgumentException: Error serializing value 'ModelClass' of type 'ModelClass.' ---> System.Runtime.Serialization.SerializationException: Type 'IMR.Model.Models.Admin.UserInOutLogSearch' in Assembly 'IMR.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.    at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)    at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)    at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()    at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, Strea...

Add Image through base64 on PDF in itextsharp

If you have base64 code image or image path, easily can add image in PDF from iTextSharp. Code below: PdfReader pdfReader =  new  PdfReader( "C:\\file.pdf" ); PdfStamper pdfStamper =  new  PdfStamper(pdfReader,  new  FileStream( "target save path" , FileMode.Create)); string  imagepath =  "base64 image code" ; Byte[] bytes = Convert.FromBase64String(Regex.Replace(imagepath, @ "^data:image\/[a-zA-Z]+;base64," ,  string .Empty)); var  pdfContentByte = pdfStamper.GetOverContent(1);  // page number iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes); image1.SetAbsolutePosition(140, 70); image1.ScalePercent(20f); pdfContentByte.AddImage(image1); pdfStamper.Close();

How to find text in dropdownlist in asp.net

Finding item in dropdownlist from a string. ddlStudent.Items.Contains( new  ListItem("Dawood"))

Remove first and last special characters from string in c#

Sometimes we need to remove special character from staring and ending of a string. private   static   string  RemoveFirstAndLastSpecialCharacter( string  input) {      if  (input !=  string .Empty)     {          string  firstRemovableStr =  string .Empty;          string  lastRemovableStr =  string .Empty;          bool  IsLetter =  false ;          for  ( int  i = 0; i < input.Length; i++)         {              if  (! char .IsLetter(input[i]) && !IsLetter)             ...

PDF not open using base64 in chrome

Solved : These options will be help full for resolving the issue. 1- Convert base64 to blob var blob = b64toBlob("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog' +   'IC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAv' +   'TWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0K' +   'Pj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAg' +   'L1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+' +   'PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9u' +   'dAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2Jq' +   'Cgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJU' +   'CjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVu' +   'ZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4g' + ...

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Solved:  When we use conditions on Eval many times we got the error, two ways use solve this error. 1- You make a function what you needs and bind before eval Example: bool ShowAge ( int age ) { return ( age > 18) ; } and call where you use eval Visible=' <% # ShowAge(Eval("Age")) %>' 2- You make a ItemCommand event and define condition on it protected void rptUser_ItemDataBound ( object source, RepeaterCommandEventArgs e ) { TextBox txtAge = ( TextBox )e.Item.FindControl("txtAge"); if ( DataBinder .Eval(e.Item.DataItem, "Age") > 18) { txtAge.Visible = false ; } ; }

how to insert text and tags at the cursor CKEDITOR

//change all "IDofEditor" to the id of your editor; I haven't figured out how to find it less explicitly //insert text CKEDITOR . instances . IDofEditor . insertText ( 'some text here' ); //insert a link (or other tags, modify as needed) //this example uses the selected text for the link text //if removing getSelection() and just inserting it all, you MUST have some link text or it will NOT insert; also if the user selected text first anyway it will change it to "NaN". //the \x22 represents a double quote, thus easy to use within an html onclick //getNative() gets the text, otherwise you get an object CKEDITOR . instances . IDofEditor . insertHtml ( '<a href=\x22my_link\x22>' + CKEDITOR . instances . IDofEditor . getSelection (). getNative () + '</a>' ); //you could also prompt the user for the link text (and title and link or anything else); very fast entry! CKEDITOR . instances . IDofEditor . insertHtml ( ...

float left or right from bootstrap predefined class

if you want to float left or right with Predefined classes of bootstrap Example:  HTML <div class="pull-left"></div> <div class="pull-right"></div> Asp.Net <asp:LinkButton ID="lnkCancel" CssClass="btn btn-primary pull-left" runat="server">Cancel</asp:LinkButton> <asp:LinkButton ID="lnkSave" CssClass="btn btn-primary pull-right" runat="server">Save</asp:LinkButton>

bind dropdownlist in repeater asp.net

Easy you set drop down list datasourse in repeater 1- Create ItemDataBound event 2- Check ListItemType one by one 3- Find dropdown which you take on repeater 4- DataSource protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)         {             try             {                 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)                 {                     DropDownList ddl = (DropDownList)e.Item.FindControl("ddlAbc");                     DataTable dt = GetDataSource();                     ddl.DataSource = lstReferencePoint;                ...

Get Web API to return plain or simple text

Often we get as a collection or object when we test API and get return to a string like a plain text or auto generated ID. It is simple to view that code  response.Content.ReadAsStringAsync().Result

The tablix 'Tablix' is invalid. The value for the DataSetName property is missing.

The tablix is invalid. The value for the DataSetName property is missing. Solved: This is simply define any datasource field to rdlc report column.its cannot be empty without any passing dataset field.

Inline if else condition in c#

Its mostly use assign only one variable or object Example : you have a variable and you want to check null value int a = 100 ; string result = string .Empty ; if ( a < 20 ) { result = "a variable less than 20" ; } else { result = "a variable not less than 20" ; } you can use instead of old condition because its a major benefit to reduce line of code int a = 100 ; string result = a < 20 ? "a variable less than 20" : "a variable not less than 20" ;

ConfigurationManager ConnectionStrings not available in c#

ConfigurationManager not show ConnectionStrings until you will not add reference System.Configuration dll, right click on Project -- Add Reference after click to Assemblies then Framework and find System.Configuration dll vs2010: Project -- Add Reference after click to .NET components and find System.Configuration dll If also add this reference but then again its not showing its a simple way to solve you add System.Configuration.ConfigurationManager.ConnectionStrings

Insert collection of data to sql table using c#

insert collection of data in SQL table is called bulk data manipulations, this is the simple and best way to add multiple row with the single collection now define a step's to easy to understand 1- You make a user defined table types  CREATE TYPE [dbo].[TTMultiRowCollection] AS TABLE(     [ID] [int] NULL,     [Name] [varchar(50)] NULL, ) 2- Create SP and take parameter table type as well as READONLY keyword. CREATE PROCEDURE [dbo].[AddMultiRow](@MultiRowCollection TTMultiRowCollection READONLY) AS BEGIN     INSERT INTO [User](ID,Name)         SELECT ID, Name FROM @MultiRowCollection END 3-  Create table in c# for passing the sql DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Rows.Add(1, "Dawood"); table.Rows.Add(2, "Ahmed"); 4- Connect to SQL server and passing the table from stored procedure  us...