Updating Campaign Member using Salesforce API and C#

Web programming
Well, if you check at Salesforce API documentation and follow their tutorial to connect to SF database and request data, everything should be fine. The API its pretty flexible and solid. But, once you get to the update point you may find some problem... I connected to Salesforce and requested my CampaignMember, loading it into an object called "CampaignMember" (yes, im so original). Then, if I tried to update it using something like: [sourcecode language="csharp" wraplines="false"] Salesforce.SaveResult[] saveResults = sf.update(new Salesforce.sObject[] { CampaignMember }); [/sourcecode] It would not work, since it seems that object has every field filled and would try to update all of them, including the read-only ones. So I would receive a saveResults object with the error message "Unable to create/update fields: CampaignId, HasResponded, LeadId. Please check…
Read More

Adding Libraries to your .Net Application Project

Web programming
Some years ago, if you wanted to divide your project with layers having a business and a "data access" layer you could create a special folder in your .Net application called "App_Code". Just doing a right Click on the project and then selecting Add -> .Net Folder -> App_Code. While that helped dividing layers some people found it less reusable since the layers where part of the project in the end, and then it became a good practice to use libraries instead to a point where the folder App_Code has disappeared from Visual Studio and you shouldn't use it anymore. So now, to have your "App_Code" you should use Class Libraries instead. You can create them separately and add them to the project or create them directly inside the project.…
Read More

Embedding images in an Email sent by ASP.Net Server

Web programming
If you want to embed your images in an email sent using ASP.Net server instead than using URLs you can use an AlternateView and use the mime standard to embed them: [sourcecode language="csharp" wraplines="false"] using System.Net.Mail; protected void SendBTN_Click(object sender, EventArgs e) { // Send email using ASP.Net: System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new System.Net.Mail.MailAddress(Email_From); message.To.Add(Email_To); message.Bcc.Add(Email_BCC); message.Subject = Email_Subject; message.IsBodyHtml = true; message.AlternateViews.Add(getS4HRegisteredUsersWithoutAnOrderBody_ForEmail(Customer.Name)); SmtpClient smtp = new SmtpClient("subdom.myserver.com"); smtp.Send(message); // Show some "Email Sent" msg and all that stuff or return ok. } private AlternateView getS4HRegisteredUsersWithoutAnOrderBody_ForEmail(String CustName) { StringBuilder body = new StringBuilder(); body = SkyGuard.MIS.Email.getTemplate("S4HRegisteredUserWithoutOrder.htm"); body.Replace("[SUBJECT]", Email_Subject); body.Replace("[CUSTNAME]", CustName); //These keys are set into the image src attribute body.Replace("[LOGO_IMGSRC]", "cid:logo"); //Notice that we are adding a cid:id as the image source body.Replace("[SUPPORT_PHOTO_IMGSRC]", "cid:avatar"); body.Replace("[SIGNATURE_IMGSRC]", "cid:signature"); AlternateView…
Read More

How can I force a page jump in HTML printing?

Web programming, Web standards
If you would like to decide when the page will break and the next one will be started when your web page is printed you can use three CSS2.0 styles to set this "jump". page-break-before: Sets the element page break behavoir before the element starts. page-break-inside: Lets you set that the page break behavior has to be avoided inside this element. page-break-after: Sets the element page break behavoir after the element ends. So, you can for example add this style to a table and the table will start in a new page when the page is printed: [sourcecode language="css" wraplines="false"] table.details {page-break-before: always;} [/sourcecode] Or you can have an style like this and use it as a page-breaker. [sourcecode language="css" wraplines="false"] .page-break {page-break-before: always;} [/sourcecode] [sourcecode language="html" wraplines="false"] <div class="page-break"></div>…
Read More

Adding an HTTP Handler to manage Images requests in .Net

Web programming
Building the Handler First, make the handler just creating a new class, make it use the IHttpHandler interface to make it "interfaceable". To make it part of the HttpHandler interface you need 2 methods: IsReusable and ProcessRequest. The method IsReusable is used to determine if the handler should be kept in memory and be reused by any request or should make a new instance for every request. In our case our handler should be reusable so it handles every request saving proc time and memory. The method ProcessRequest will get the request and send a response back, obviously. [sourcecode language="csharp" wraplines="false"] public class UserImagesHandler : IHttpHandler { public UserImagesHandler() { } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request;…
Read More

Using global objects in your page with a Master Page

Web programming
MasterPages are very useful to set the site structure like a header and footer, but, they can also help managing authorization or global issues of the site just declaring the needed variables on it and using them, if necessary, from the content pages or user controls. Let's see this with an example, imagine we need to add a site navigator object for showing dynamic menus, a navigator and other issues. First of all, we need to declare that variable as a public property: [sourcecode language="csharp" wraplines="false"] public MyLibrary.Navigator Navi {get; set;} [/sourcecode] We need to initialize the object (because we are using an object in this example) before accesing it or we'll find a null reference. If we do that in the Page_Load, we will find that the Page.Page_Load loads…
Read More

Linking a .Net Label Control with an input

Web programming
In standard HTML label tags are used to assign some text to an input inside a form, giving the user the option to click the text in the label and causing the assigned input to be clicked too. This is very useful for CheckBoxes and Radio Buttons. [sourcecode language="html" wraplines="false"] <!-- Standard HTML4.1 --> <input type="checkbox" id="CHKCandies" value="1" /> <label for="CHKCandies">I want some candies</label> [/sourcecode] In .Net you can always assign some text to a CheckBox or Radio Button using its Text property, but, in case you want to add some functionality to that text like a link in the typical "[ ] I'm agree with the <a>Terms of use</a>" or similar you may find that the Text property in a Radio Button isn't enough. Fortunately the Label control not…
Read More

Paginate Grid Views in C#.Net

Web programming
Standard pagination To paginate a Grid view you only need to set the property "AllowPaging" to True and set an event for "PageIndexChanging". You can do all this just using the properties panel. Once you have it done you need to set the gridview actual IndexPage and DataBind() it again, something like this: [sourcecode language="csharp" wraplines="false"] protected void ResultsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) { ResultsGrid.PageIndex = e.NewPageIndex; LoadResults(); //This will redatabind the GV } [/sourcecode] Those easy steps will give you a Grid View with standard pagination, you can modify some of its properties like: AllowPaging: Remember this property has to be set to True or pagination will not be enabled. PageSize: Lets you decide how many rows will be shown per page. Position: Where to show the page navigator: (top,…
Read More

Load dynamic Web User Control in .Net

Web programming
One of the most common controls used in .Net is the Web User Control, which is in fact a website piece to be reused in different pages. You normally place them in the page while developing it just dropping it into the page and then using them as part of it, but, what if you want more than one of them in the same page and you don't know how many of them you are going to use? For doing that you can always set 10 or more of them in the page and then show as many as you need or, you can dynamically load as many of them as you need this way: First, I'm going to place the control in a namespace, this is not necessary, but…
Read More

Resizing image to some disk space in C#.Net

Web programming
Ive seen lots of tutorials of how to resize an image to some height or width, but none of them explain what to do in case you need to reduce its disk space to a limit of, say, 200Kb. This may not be the best way of dealing with this but it works: Getting and checking the image [sourcecode language="csharp" wraplines="false"] //Lets imagine im uploading an image from a .Net FileUpload object: System.Drawing.Image img = System.Drawing.Image.FromStream(Uploader.PostedFile.InputStream); //Now, I check the size limit: if (ImageSize(img) > 200){ img = ResizeImageTo200K(img); } [/sourcecode] Resizing it I just make a bucle to resize the image until its less than 200k or I may have get into an infinite loop: [sourcecode language="csharp" wraplines="false"] private System.Drawing.Image ResizeImageTo200K(System.Drawing.Image img) { int control = 0; while ((ImageSize(img)…
Read More

How to add the AJAX Control Toolkit to your project

Web programming
AJAX Control Toolkit is an extension made for ASP.Net that you can install for using many different AJAX controls in your website. They don't come with the basic Visual Studio installation so you have to install them if you want to use them (the AJAX core with update panels and timers comes with Visual Studio). Copied from ajax.net, just to save a copy! Download AJAX Control Toolkit from here. Extract the files and place the folder with them in a safe place. Launch Visual Studio and create a new ASP.NET Web Forms project or website. Open Default.aspx in the Visual Studio editor. Create a new Toolbox tab by right-clicking the Toolbox and selecting Add Tab. Name the new tab Ajax Control Toolkit. Right-click beneath the new tab and select the…
Read More

Add new post Types and Fields to our wordpress blog

Web programming
Hi there! Have you ever thought to have another field in the wordpress post edit page, for example, an image field to set a thumbnail for that post, a price field to set the price of the houses you are showing or, who knows, just a subtitle field to place a text down the post title? Whatever you have imagined now is possible in wordpress 3.0+ using a few plugins and... coding a bit. Let's start by downloading the needed plugins: More Types More Taxonomies More Fields All of them are independent but can work together very well. I don't want this post to be so long so, I'm gonna let you install those plugins and check them a bit until you realize how they work. More Fields lets you,…
Read More

Using preprocessor directives in C#.Net

Web programming
We can use conditional directives in C# that will be executed at the pre-compiler to allow us determine which code will be compiled or not: #if and #endif. To sue them, we need to define a symbol. What do you mean with a symbol? A symbol is like a variable that we will use for this purpose. You can declare one of those using #define or #undef and use it later to add some pre-compiler conditional directives. Let's see an example: [sourcecode language="csharp" wraplines="false"] #define DEVELOPMENT_MODE #undef PRODUCTION_MODE public class OleDB { public static OleDbDataReader ExecuteReader(string cmdText){ #if PRODUCTION_MODE srcDB = "c:\\server\\databases\\bd1.mdb"; #else srcDB = "c:\\bd1.mdb"; #endif } } [/sourcecode] As a mere example, see how we could use a pre-compiler directive to set the database source when we are…
Read More

Connecting OleDB using ADO.Net (C#)

Web programming
Connecting OleDB in .Net is no different than connecting other database engines, you only need to use a Connection object, a Command object and then make the operations you were planning to do. First of all, you should import the libraries we need for connecting OleDB, so if for connecting to SQL Server you use System.Data.SQLServer, now you will use System.Data.OleDB: [sourcecode language="csharp" wraplines="false"] using System.Data.OleDb; [/sourcecode] Now we'll need a ConnectionString in which the connection details will be set, and which has a different structure for OleDB than for SQL Server. Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataBaseFileSource;User Id=Admin;Password='' And here we go: [sourcecode language="csharp" wraplines="false"] public static string srcDB = "c:\\development\databases\bd1.mdb"; public static OleDbDataReader ExecuteReader(string cmdText){ OleDbConnection conex = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcDB + ";User Id=Admin;Password=''"); conex.Open(); } [/sourcecode] As you can…
Read More

Inheritance in C#.Net

Web programming
Inheritance is the posibility of making a class act as a child of another and inherite its parent methods and properties. So if you have a class called building with the property address and you make another class called flat, you can make flat a child of building and it will inherit the property address without having to declare it. To make flat inherit building you only need to do something like this: [sourcecode language="csharp" wraplines="false"] public class Flat : myClasses.Building { // Here goes the class code } [/sourcecode] Assuming that the class building is inside the namespace MyClasses. But flat will not inherit everything building has, the private methods or properties will not be inherited. Does this means I have to declare everything as public if I want…
Read More