Monday, February 21, 2011

Get gridviewrow selected index in Row Command Event

In your row command event, add a code like this:
int selected_index = Convert.ToInt32(e.CommandArgument);  

Tuesday, February 15, 2011

Cast SQL Money field type to C# Type

Given you have a field called customer_salary which is of type money in sql server, to cast and use it in your c# programs, you cast it to decimal.
decimal Price = Convert.ToDecimal(reader.GetValue(1)); 

Thursday, February 10, 2011

Choosing between ASP.NET MVC and ASP. NET Webforms

I've been playing around mvc just recently, i was really shocked and somewhat struggling with this new framework since i was used to the traditional N-Tier development with As I developed simple examples, mvc is somewhat similar to n-tier development. As such, each layers have been separated to enable designers and coders to work independently with each layer. I have some books in my arsenal but i haven't read them yet.LOL

Basically, I'm still a newbie with this stuff. One tip that i read was choosing between webforms and mvc. This will provide insights on other developers as well.

Choosing between ASP.NET MVC and ASP.NET Webforms

Source: ASP.NET MVC 1.0 Quickly

In general, choosing between ASP.NET MVC and ASP.NET can be based on the
following five criteria:

1. Are you considering test-driven development (TDD)?
TDD enables you to write tests for an application first, after which the
application logic is developed. An ASP.NET Webforms application uses only
one class to display output and handle user input. Automated testing of this
class is complex as you are required to load the full ASP.NET stack into a
separate process (for example, in IIS). The MVC framework allows the testing
of each component separately, without requiring the full ASP.NET stack. For
example, you can test your model separately from your controller without
requiring a view. If you are considering TDD, the ASP.NET MVC framework
will be the better choice.

2. Is there a need for fine control over HTML markup?
ASP.NET Webforms automatically inserts hidden HTML markup, IDs,
JavaScript, and so on, into your page's HTML output because of its
event‑driven architecture and its use of ViewState. The ASP.NET MVC
framework allows for 100% control over the HTML output. If you require full
control over your HTML markup, the ASP.NET MVC framework will be the
better choice.

3. Is the application heavily data-driven?
If you are developing an application that is heavily data-driven and is using
grids or a lot of master-detail editing of data, ASP.NET Webforms may
be the better choice as it provides a lot of controls that will aid you in the
development of these kind of applications. Of course, you can use the
ASP.NET MVC framework for these tasks too, but you will be required to
write more code to achieve the same goal.

4. Is there a need for a Winforms-like development approach?
Does your development team write Winforms code? Is there a need for an
event-driven programming approach? In these cases, consider ASP.NET
Webforms in favor of ASP.NET MVC.

5. Is there a need for a rapid application development (RAD)
development approach?
Does your client expect a quick prototype of an application? Is the use of
drag-and-drop development using pre-created web controls required? If so,
consider ASP.NET Webforms in favor of ASP.NET MVC.

As noted, ASP.NET MVC is not a replacement with ASP.NET WEBFORMS!!!

Sunday, February 6, 2011

Calculate Age in SQL Server

Here is a script to calculate an age of a person using TSQL using Convert().
1:  SELECT FirstName, LastName   
2:    ,CONVERT(Int, DATEDIFF(day, BirthDate,   
3:    GETDATE())/365.25) As Age   
4:  FROM EmployeesDTR  

Source: TSQL Reference

Thursday, February 3, 2011

Login Control Never Redirects to Index or Default page

Working with login control in provides convenient way to apply secured user API with less programming. Since login control is part of .net framework,such classes are provided such as membership, forms authentication and etc..

One weird error i encountered is that, it does not redirect to my index page once login has been successful. I implemented my own validation function to check if a user exists in the database. If the method returns true, redirect to index page. See code below. But the fact is, it does not redirect.
  if (CheckUserInMyDB(Login1.UserName, Login1.Password))  
     e.Authenticated = true;  
     Login1.Visible = false;  
     e.Authenticated = false;  
So the solution i found in was to transfer the Response.Redirect to Logged in method of the login control.

Tuesday, February 1, 2011

Insert Parameter Array in .NET

Here's a snippet to insert a parameter array in .NET:
   1:    ' add a class ex. Patient  
   2:      Public Class Patient  
   3:        Public name As String  
   4:        Public code As Integer  
   5:      End Class  
   6:      ' create a delegate   
   7:      Delegate Function myConn() As String  
   8:      'function to return connstring  
   9:      Private Function connectionstring() As String  
  10:        Dim conn As String = "your_connection_string here"  
  11:        Return conn  
  12:      End Function  
  13:      '--Main Sub  
  14:      ' execute insert array    
  15:      Dim objCon As myConn  
  16:        objCon = New myConn(AddressOf connectionstring)  
  17:        Dim str As String = objCon.Invoke()  
  18:        Dim connection As New SqlConnection(str)  
  19:        Try  
  20:          Dim patients As New List(Of Patient)  
  21:          For i As Integer = 0 To max_count_variable  
  22:            Dim patient As New Patient()  
  23:   = "greg nelson" + i.ToString  
  24:            patient.code = i * 2  
  25:            patients.Add(patient)  
  26:          Next  
  27:          connection.Open()  
  28:          Dim query As New StringBuilder  
  29:          Dim cmd As New SqlCommand  
  30:          For j As Integer = 0 To patients.Count - 1  
  31:            query.Append(String.Format("Insert into patients  
  32:               (PatientName,PatientSexCode)   
  33:               values(@names{0},@code{1});", j, j))  
  34:            'declare the parameters  
  35:            cmd.Parameters.AddWithValue(String.Format  
  36:             ("@names{0}", j), patients(j).name)  
  37:            cmd.Parameters.AddWithValue(String.Format  
  38:             ("@code{0}", j), patients(j).code)  
  39:          Next  
  40:          cmd.Connection = connection  
  41:          cmd.CommandText = query.ToString()  
  42:          cmd.ExecuteNonQuery()  
  43:        Catch ex As ApplicationException  
  44:          Throw New Exception(ex.Message)  
  45:        End Try  
  46:        connection.Close()  
  47:        Console.Write("Array successfully saved")  
  48:        Console.ReadLine()  
  49:      '--end main sub