C#

Invalid postback or callback argument ASP.NET

I got this error

I got this error because I was using a ready made html5 template. The problem was duplicate form tag used multiple times in the template. Removing that get rid of the error.

To be exact my duplicate form tag was in the master page which was a bit hard to find.

Looping through gridview in C# always return empty

I had a gridview which was bounded to a datasource (datatable in my case) in code behind in C#. Code was something like this

This was always returning empty (”) no matter which column index I was pointing to. I tried different variations like

but it failed too. After pondering on it for quite a while and still not resolved, I decided to loop through the data source itself, in my case a datatable. That actually worked! That code looks like this

I am still not sure why it will not read the values from GridView in a loop but at least there is work around.

invoke delegate with parameters

Download Ping Example Code (10kb)

The following is a simple code to invoke a delegate with parameter. I created this to update label control on the form asynchronously. I did not quite work I was hoping for but the code does pass parameter to the delegate

RAISERROR and Exceptions

RAISERROR with severity level below 10 are not catched by ADO framework. To fix it simply raise severity level to 10 or above or you can instead use THROW statement which has severity level of 16 by default and will always be catched by the ADO framework (C#).

RAISERROR('Your Error Message', 'Severity Level', 'State Level')

Set Severity Level to 16
Set State Level to 1

Note that RAISERROR does not stop execution. It is recommend to use THROW statement inst5ead to raise error and stop execution.

Does RAISERROR cause Transaction ROLLBACK

executenonquery returns -1 on success

ExecuteNonQuery is suppose to return the number of rows inserted, updated or deleted but in my case it always return -1 even though the record is successfully added.

Found out

SET NOCOUNT OFF

was missing my in my stored procedure. If you have multiple SQL statements in your stored procedure, initially set it to on by

SET NOCOUNT ON

which will not count any rows affected. Later to monitor the statement you are actually interested in, set it to OFF.

Hope that resolves your problem!

Spring.NET Constructor Injection Example

The following Console Application (C#) represents Constructor injection using Spring.NET. To use the code in as it is form, create a console application using C# in Visual Studio 2012 and name it Spring4. Copy the content of program file from below. Create another xml file named context.xml and add it to your project. Copy the contents of that file from what I have supplied. Last but not the least, download spring.net core from NUGET. You only need the Spring.net core, you can ignore the rest.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring;
using Spring.Context.Support;

namespace Spring4
{
    class Program
    {
        /// <summary>
        /// This program demonstrates constructor injection using Spring.NET
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var context = new XmlApplicationContext("../../context.xml");

            Writer wall = (Writer)context.GetObject("writer");
            wall.WriteMessage();

        }
    }

    public class WriteMessage
        {
            WriteMessage(IWritingDevice device)
            {
                Console.Write("I am writing with " + device.DeviceName());
            }
        }

        public class Writer
        {
            private IWritingDevice _device;
            Writer(IWritingDevice device)
            {
                _device = device;
            }

            public void WriteMessage()
            {
                Console.Write("I am writing with " + _device.DeviceName());
            }
        }

        interface IWritingDevice
        {
            string DeviceName();
        }

        public class Pen :IWritingDevice
        {
            public string DeviceName()
            {
                return "Fountain Pen";
            }
        }

        public class Pencil :IWritingDevice
        {
            public string DeviceName()
            {
                return "Grey Pencil";
            }
        }
}

Context.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object id="writer" type="Spring4.Writer,Spring4" singleton="false">
    <constructor-arg index="0" ref="Pen"/>
  </object>

  <object id="Pen" type="Spring4.Pen, Spring4" />
  <object id="Pencil" type="Spring4.Pencil, Spring4" />
</objects>

Explanation

In this example, I am using a writer Object to write something. But the writer object cannot write by itself. It needs a writing device to write with. It is required that is supplied the object in the constructor.

My Writer Object is dependent on writing device, which must implement IWritingDevice. We can pass anything to this class which is derived from IWritingDevice interface. In the given example, I trying to write with fountain Pen.

Fountain Pen or Simply Pen is passed to the constructor of Writer Object. This is configured in context.xml. Notice that you cannot just change the constructor argument on the fly, it is configured in XML file in Spring context and will always be the same.

Notice that no new kewyord is used. When Writer Object is created, Spring automatically knows that a IWritingDevice object is needed for Writer Object so it it can write with something.

Some useful links

Type of dependencies

Accessing Local Resources in MVC

You want to localized your resource or in other words put them in a resource file in the local folder, how do you access it in MVC?

Local resource demo

 

Here is how you will access it

    public class Student
    {
      // Constuctor
        public Student()
        {
            Name = MyStrings.name1;
        }

        public string Name { get; set; }
    }

A better way of doing it to put all resources in inside a separate folder and give them proper extension depending on where they will be used. For Example I can use
>>LocalizedString
>>>>>MyStrings.Resx
>>>>>Mystrings.en-GB.Resx

The program will automatically pick the right resource depending on the culture setting of the computer. In this case you will have to include using statement to point to the resource file.

using LocalResource.Models.LocalizedStrings;