Friday, November 20, 2009

Invalid memory location error with PHP, IIS and MS SQL/ My SQL

Invalid memory location error with PHP, IIS 5.1 and MS SQL Server 2005

There can be multiple reasons for this error:

@ Please recheck all the configurations settings that are suggested in PHP।CONFIG file that have been suggested when you install PHP on IIS Server with MS SQL Server 2005.

@ Also, check that you have loaded all extension dll’s in the phproot\ext\ folder and you have installed MS SQL server driver for PHP.

@ The most common reason for this error is because of the “ntwdblib.dll”. If you decide to use MS SQL server with PHP, you need to replace the default version of ntwdblib.dll file with the version number 2000.80.194.0. You can either get this MS SQL installation discs. Install the prerequisite files from Disc1 and it will install all needed dll’s. Or, you can download it from http://webzila.com/dll/1/ntwdblib.zip.

@ Run IISRESET command and then check your application.

@ If you are getting this error message with the My SQL, then it may be the case that the My SQL version is not compatible with the PHP version and you will need to upgrade My SQL to the higher version.

Thursday, September 24, 2009

ASP.NET 3.5 - Populate grid view based on value selected in the dropdown

Q:
Can we populate grid view dynamically based on dropdown value?
I have a dropdown list. When user selects a particular value from the dropdown list, the grid view records needs to be changed based on the value selected in dropdown.

A:
Continuing to earlier post where I have demonstrated how we can bind dynamic dataset to the grid view; I am going to explain how we can achieve this.

Task:
I have a dropdown list for divisions e.g. 1st year, 2nd Year etc. If user selects 1st Year from the dropdown, students list for Division 1 will be displayed in the grid view. If user selects 2nd year, they will be presented 2nd year student list.

Below are the steps that we will have to follow to achieve the given task:
1. Add dropdown list for all division values
2. On change of the selected index, bind new dataset to the grid view
3. It is important to enable “AutoPostBack” property for the dropdown field

Step 1:
Below is the code snippet to add dropdown field for all divisions on the form:

<div style="margin-top:10px;">
<asp:Label ID="lbl_SelectDivision" runat="server" Text="Select division:"> </asp:Label>
</div>
<div style="margin-top:10px;" >
<asp:DropDownList ID="ddl_Division" runat="server" >
<asp:ListItem Text="I st year" Value="ISTYEAR" />
<asp:ListItem Text="II nd year" Value="IINDYEAR" />
</asp:DropDownList>
</div>


This adds 1st Year and 2nd year as two division values to the dropdown list.

Step 2:
The next is to add an event which will be called on change of the value selected in the dropdown. The name of this event is “onselectedindexchanged”. We will call a method defined in the code behind class for this page. [You can achieve the same using design view. If you go to property window for the dropdown and double click on the event name, IDE automatically add this function for you.]

<div style="margin-top:10px;">
<asp:Label ID="lbl_SelectDivision" runat="server" Text="Select division:"> </asp:Label>
</div>
<div style="margin-top:10px;" >
<asp:DropDownList ID="ddl_Division" runat="server" onselectedindexchanged="ddl_Division_SelectedIndexChanged" >
<asp:ListItem Text="I st year" Value="ISTYEAR" />
<asp:ListItem Text="II nd year" Value="IINDYEAR" />
</asp:DropDownList>
</div>


Here is the code behind method. This function has a duty to collect the selected value of the dropdown and pass it to helper function which does work to dynamically populate records in grid view.

/*
*This event handler simply collect current selected value of the dropdown
*Passes it to the helper function which will actually populate dynacmic data to grid view
*/
protected void ddl_Division_SelectedIndexChanged(object sender, EventArgs e)
{ PopulateStudentsList(ddl_Division.SelectedValue.ToString());
}


PopulateStudentsList” is the method which we have seen in earlier post modified a little bit to accept the selected value as a parameter and use it for further processing.

public void PopulateStudentsList(string pm_selectedDivision)
{
gv_StudentsList.DataSource = getStudentList4Division(pm_selectedDivision);
gv_StudentsList.DataBind();
}

Step 3:
You are yet now done. Up to now, you have all source code ready to wok fine for your requirement. But the change in dropdown value will not call your event handler defined in step 2.
In order to fire this event handler, you must submit the page whenever you change the dropdown value. This is achieved using “AutoPostBack” property set to true for the dropdown field. “AutoPostBack” property if enabled - helps you to submit your page automatically whenever there is a change or action has been made on the control.

<div style="margin-top:10px;">
<asp:Label ID="lbl_SelectDivision" runat="server" Text="Select division:"> </asp:Label>
</div>
<div style="margin-top:10px;" >
<asp:DropDownList ID="ddl_Division" runat="server" onselectedindexchanged="ddl_Division_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Text="I st year" Value="ISTYEAR" />
<asp:ListItem Text="II nd year" Value="IINDYEAR" />
</asp:DropDownList>
</div>


Here is the result after you change the value from 1st Year to second your.



Note: If you do not want to submit the page on change of dropdown value; best approach is to use AJAX – next topic of discussion :)!

Monday, September 21, 2009

ASP.NET 3.5 - How to populate grid view dynamically using dataset

Q:
I have a req. where I want to display the list of students. The records to be displayed are based on the filtered criteria; also the columns to be displayed in the grid are decided at run time.

A:
You have a grid view defined in your page as below:

/* grid definition - spaces are added to render html tags as text
between tags */
<div style="margin-top:10px;" >
<asp:Label ID="lbl_StudentsList" runat="server" Text="Student's List:" >
</asp:Label>
</div>

<asp:GridView ID="gv_StudentsList" runat="server" >
< emptydatarowstyle backcolor="LightBlue" forecolor="Red"/>
<EmptyDataTemplate>

<asp:Label runat="server">No student found. <
/asp:Label>

</EmptyDataTemplate>
</asp:GridView>
</div>

You have to call the method in an event on which you want to populate the records in grid view. I will call this method (Method definition is given below) on load event of the page.
/* Call method on particular event */
protected void Page_Load(object sender, EventArgs e)
{
PopulateStudentsList();
}

public void PopulateStudentsList()
{
/* This defines data source for the grid as dataset returned from the
method and then bind it to grid to see the records in the grid in action */
gv_StudentsList.DataSource = getStudentList4Division("ISTYEAR");
gv_StudentsList.DataBind();
}

Now, we will write a method which will populate grid view with data and the columns to be added - defined dynamically inside the method.
/* This method constructs data set using data table */
public DataSet getStudentList4Division(string pm_strDivision)
{
DataSet dsListStudent = new DataSet();
DataTable dt = new DataTable();

/* This example works but is not the standard way to populate data from
DB – this is just for demonstration purpose
You need a database named
Quickstart in your backend with Student table having schema as columns specified
as per req. */

String connString = "Data Source=.;Initial
Catalog=QuickStart;Integrated security=true";

SqlConnection sqlConnect =
new SqlConnection(connString);
sqlConnect.Open();
SqlCommand cmd = new
SqlCommand("select * from Student WHERE division ='" + pm_strDivision + "'",
sqlConnect);
SqlDataReader sdr = cmd.ExecuteReader();

if
(sdr.HasRows)
{
//Prepare data table columns
string strfirstname =
"";
string strlastname = "";
DateTime strdob;
string strcity = "";
string strreligion = "";

/* These are the column headings */
dt.Columns.Add("First name", typeof(string));
dt.Columns.Add("Last
name", typeof(string));
dt.Columns.Add("Date of birth", typeof(DateTime));
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Religion",
typeof(string));

while (sdr.Read())
{
strfirstname =
sdr["FirstName"].ToString();
strlastname = sdr["LastName"].ToString();
strdob = Convert.ToDateTime(sdr["DateOfBirth"]);
strcity =
sdr["City"].ToString();
strreligion = sdr["Religion"].ToString();

/*
This line populates value in the data table */
dt.Rows.Add(strfirstname,
strlastname, strdob, strcity, strreligion);
}

}

//Add Table
to dataset
dt.TableName = "Students list";
dsListStudent.Tables.Add(dt);

sdr.Close();
sqlConnect.Close();

return dsListStudent;
}
The result of the data source in the grid view will look as below:

In next post, we will see how to populate grid based on the value selected in the dropdown list using the above scenario.

*Please note that the code explored above is working but has not been validated for different unit tests like positive test or negative test.

*Schema for the table student is as below:
CREATE TABLE [dbo].[Student](
[ID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[FirstName] [varchar](80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[LastName] [varchar](80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DateOfBirth] [datetime] NULL,
[City] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PanNo] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Religion] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Cast] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Division] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

Thursday, September 17, 2009

C# - Hashtable and ArrayList

Q:
What is “Hashtable”? What are the benefits of it over Arraylist?
I am thinking of using Arraylist or Hashtable to group and then sort near about 100000 of records. Which collection will be better option for me? Why?

A:
Hashtable is one type of collection supported by C#. It’s a collection of items in key value pair. Every item in the Hashtable has a key with pointer to its linked value. The key and the value can be any object. Key must be unique value. Hashtable is useful when you need to store data in a key and value pair.

A Hashtable is better for looking up values based on the key, but you can't really sort it - it maintains its own 'internal sort' to optimize the lookups.

So, if you want to lookup values then it's better to use a Hashtable, if you want to sort all the items into a particular order then use the Arraylist.

A wrapper class can be designed that will make use of internal hashtable to provide search on items and a list for iteration and sorting.

Trick for rapid fire developers to get items sorted from hashtable:
Hashtable h = new Hashtable();
h.Add(1, 1);
h.Add(2, 5);
h.Add(3, 3);
ArrayList l = new ArrayList(h.Values);
l.Sort();
*Values in hashtable should be numeric values.

.NET 2.0 also offers Dictionary collection. It is a mix between hashtable (sort and identify objects of any type by a key) and an Arraylist (objects of a specified type).