IRohitable | .NET, SQL Server, Ruby on Rails and everything related to Software

How to call a non-static method from a static method?

by Rohit 7. December 2014 22:12

To call a non-static method from a static method:

  1. Create an instance of the class
  2. Call the non-static method
class MyClass
    {
        static void Main(string[] args)
        {
            Method1();
        }

        static void Method1()
        {
            MyClass p = new MyClass();
            p.Method2();
        }

        void Method2()
        {
            Console.WriteLine("Called from Static Method");
            Console.Read();
        }
    }

Tags: ,

C# | OOP

Few cool features of ASP.NET 5

by Rohit 23. November 2014 22:53

Built from the ground up to provide optimized framework for application development, ASP.NET 5 includes several new features and enhancements. There are few cool features which are of interest to me:

1. Cross-platform runtime for Linux and Mac OS X: This runtime will enable developers to build .NET apps on Mac and Linux devices. We will see more development in the Mono platform.

2. Open-source and Transparent: Entire ASP.NET code is available on GitHub. The GitHub repository is actively used by the ASP.NET team for all the development where you can find latest code changes.

3. Cloud ready

4. Improved HTTP Performance

5. Agile development environment: You don't need to rebuild the project each time you do code changes. Now you can make changes in the code, save the changes, simply refresh the browser and see the new changes. The code can be in a class-library or in your web project.

Tags:

.NET | ASP.NET

How to add auto-incremented column in DataTable at any position?

by Rohit 18. November 2014 22:53

In this post I will show you:

  1. How to add a new column at a certain position in a DataTable?
  2. How to make it an auto-incremented column?

 using ASP.NET/C# with SQL Server.

Almost always, we have a DataTable which gets populated by stored procedure results. At times we want to add an auto-incremented column from code-behind when it is not possible to alter stored procedure.

Let us create a sample table with the name: MonthData

CREATE TABLE [dbo].[MonthData](
	[Name] [varchar](6) NULL,
	[Year] [int] NULL,
	[Month] [int] NULL,
	[Value] [int] NULL,
	[Code] [int] NULL
) ON [PRIMARY]

 

Insert some rows in this table:

INSERT [dbo].[MonthData] ([Name], [Year], [Month], [Value], [Code]) VALUES (N'John', 2001, 5, 235, 9909)

INSERT [dbo].[MonthData] ([Name], [Year], [Month], [Value], [Code]) VALUES (N'Samson', 2003, 6, 1299, 989)

INSERT [dbo].[MonthData] ([Name], [Year], [Month], [Value], [Code]) VALUES (N'Alex', 2002, 3, 4322, 7392)

 

Now create a new ASP.NET Web Form with the following HTML code:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" Font-Names="Calibri" Font-Size="Medium"></asp:GridView>
    </div>
    </form>
</body>

 

In the Page Load event, add this simple code:

protected void Page_Load(object sender, EventArgs e)
        {
            string ConnectionString = "Data Source=.;Initial Catalog=Learn;Integrated Security=True";
            string Query = "SELECT [Name], [Year], [Code] FROM [MonthData]";

            DataTable dt = new DataTable();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand(Query, connection);
                connection.Open();
                using (SqlDataAdapter a = new SqlDataAdapter(command))
                {
                    a.Fill(dt);
                }
                connection.Close();
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

Executing the above code gives you results something like this:

 

Now let us add a Row Id column in the DataTable. Also, I want to show it at position 0 i.e, first column. To do this, modify the code as below:

protected void Page_Load(object sender, EventArgs e)
        {
            string ConnectionString = "Data Source=.;Initial Catalog=Learn;Integrated Security=True";
            string Query = "SELECT [Name], [Year], [Code] FROM [MonthData]";

            DataTable dt = new DataTable();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand(Query, connection);
                connection.Open();
                using (SqlDataAdapter a = new SqlDataAdapter(command))
                {
                    DataColumn Col = dt.Columns.Add("RowId", typeof(int));
                    Col.SetOrdinal(0);
                    dt.Columns["RowId"].AutoIncrement = true;
                    dt.Columns["RowId"].AutoIncrementSeed = 1;
                    dt.Columns["RowId"].AutoIncrementStep = 1;
                    a.Fill(dt);
                }
                connection.Close();
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

On executing this code you get:

In SQL Query I have intentionally not used Row_Number function to get Row ID because I want to illustrate how this auto-increment number can be generated from code-behind.

Tags: ,

.NET | ASP.NET | C#

About Rohit Prakash

Software Craftsman and Technology Enthusiast (not a Guru).

Technical Reviewer of a book on open-source programming IDE.

My day job keeps me engaged with Microsoft Technologies (ASP.NET, C# and SQL Server) hence most of the posts are related to these technologies. I, however, love to play with other open-source technologies. I also have interest in IT Security and therefore you will find posts on Malware Analysis, Cryptography and Anti-Virus programs.

Few people have contacted me for Guest Posts. You will find these posts as well.

 

You can reach me at:

rohit [at] irohitable.com

-----------------------------------------

LinkedIn:

in.linkedin.com/in/rohitpkhare

Twitter:

@BuzzRohit

-----------------------------------------
Google+

Search with this name: Rohit Prakash

Month List

Protected by Copyscape Web Plagiarism Software