Tuesday, September 24, 2013

Typed and UnTyped Dataset


Untyped dataSet you can created in following manner by creating object of DataSet Class.

Dataset has one table with column name firstName.

DataSet ds = new DataSet();

DataTable dt = new DataTable();

DataColumn one = new DataColumn("FirstName");

dt.Columns.Add(one);

ds.Tables.Add(dt);


You can add typed dataset by following manner,

On Solution Explorer, RightClick -> Add New Item then select DataSet.xsd after that from server explorer you can drag the object or you can manuall also create table. and give "Select Command, InsertCommand , UpdateCommand and DeleteCommand", when you call the method, DataSet.AcceptChanges(); , respective command will be executed based on the Rowstate.

Dynamically Enable or Disable Required Field Validator


To enable or disable the required field validator control based on any selection
Enable or Disable ASP.Net Validation on client side

//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);

//Explanation:
ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)

//Example:
rfvOther: is a Required Field Validator
ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false);


Explonation#2:
'ValidatorEnable' or 'ValidatorUpdateDisplay' will immediately validate the associated control and show any validation messages. If this is not wanted because you just want to toggle the enabled/disabled switch but wait until form submission to validate, then the 2nd method of calling enabled on the object is the preferred method. If you do want immediate validation, then this can be done in a single line of code passing in the ID of the RequiredFieldValidator as displayed below:
ValidatorEnable($get('<%=RequiredFieldValidator1.ClientID %>'), true);
However, if you want only to enable/disable the validator, use the code below and do not make any additional calls to the built in JS functions. The error from previous posts states to set the .enable property yet there is no such thing. You must set the .enabled property on the server control. The code below shows this:
var validator = $get('<%=RequiredFieldValidator1.ClientID %>');
validator.enabled = true;

Monday, September 23, 2013

Difference between DataTable and DataView

Simply, DataView is customized view of a DataTable for sorting, filtering, searching, editing, and navigation. The DataView does not store data, but instead represents a connected view of its corresponding DataTable.

when you want to run a query and show the subset of data in a control, a DataView could help you.

DataTable

A datatable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way. The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and the DataView.

DataView

A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control.

Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data. For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property which returns the default DataView for the table.

Example:
using(SqlConnection cn = GetConnection())
 {
     cn.Open();
     SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers", cn);
     DataTable dt = new DataTable();
     da.Fill(dt);

     // At this point dt is filled with datarows extracted from the database in no particular order 
     // And the DefaultView presents the same record organization (or lack of), but...

     // Order on the default view by CustomerName
     dt.DefaultView.Sort = "CustomerName";
     foreach(DataRowView rv in dt.DefaultView)
          Console.WriteLine(rv["CustomerName"].ToString();

     // A new dataview with only a certain kind of customers ordered by name
     DataView dvSelectedCust = new DataView(dt, "CreditLevel = 1", "CustomerName", DataViewRowState.Unchanged);
     foreach(DataRowView rv in dvSelectedCust)
          Console.WriteLine(rv["CustomerName"],ToString();



 }

Difference between DataView and DataTable
DataView

1.Read-only i.e., DataView can be used to select the data.
2.Is a reference to an existing DataTable. Cannot be populated from scratch; must be instantiated with a reference to an existing DataTable.
3.Data is a reference to an existing DataTable, and does not consume space.
4.Can sort or filter rows without modifying the underlying data. Rows and columns can be hidden and revealed repeatedly.
5.Can return a DataTable version of the view
6.A live reference to a DataTable; any changes in the DataTable data is immediately reflected in the view.
7.Supports calculated columns, which are columns with a value calculated on the fly by combining or manipulating other columns.
8.Can hide or show selected columns

DataTable

1.Read/Write i.e., Datatable can be used to edit or select or delete or insert a data.
2.Can be created empty and then populated
3.Data takes storage space.
4.Can add/edit/delete rows, columns, and data, and all changes are persistent.
5.Can be cloned
6.Is source data; does not contain references
7.Does not support calculated columns
8.No row or column hiding


Thanks,
Reference from Forums,Sites.google,Stackoverflow.