Convert DataReader to DataSet in c#

Monday, 5 October 2009 15:35 by myro

I've just founded an usefull code snippet that returns a DataSet from a DataReader. As you can see, I'm passing a DbDataReader, because I prefer to work with the DbProviderFactory instead of using directly the System.Data.Sql namespace. A replace of the DbDataReader into SqlDataReader, will work exactly the same.

public static DataSet convertDataReaderToDataSet(DbDataReader reader)
{
    DataSet dataSet = new DataSet();
    do
    {
        // Create new data table
        DataTable schemaTable = reader.GetSchemaTable();
        DataTable dataTable = new DataTable();

        if (schemaTable != null)
        {
            // A query returning records was executed
            for (int i = 0; i < schemaTable.Rows.Count; i++)
            {
                DataRow dataRow = schemaTable.Rows[i];
                // Create a column name that is unique in the data table
                string columnName = (string)dataRow["ColumnName"];
                // Add the column definition to the data table
                DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                dataTable.Columns.Add(column);
            }
            dataSet.Tables.Add(dataTable);
            // Fill the data table we just created
            while (reader.Read())
            {
                DataRow dataRow = dataTable.NewRow();

                for (int i = 0; i < reader.FieldCount; i++)
                    dataRow[i] = reader.GetValue(i);

                dataTable.Rows.Add(dataRow);
            }
        }
        else
        {
            // No records were returned
            DataColumn column = new DataColumn("RowsAffected");
            dataTable.Columns.Add(column);
            dataSet.Tables.Add(dataTable);
            DataRow dataRow = dataTable.NewRow();
            dataRow[0] = reader.RecordsAffected;
            dataTable.Rows.Add(dataRow);
        }
    }
    while (reader.NextResult());
    return dataSet;
}

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (1) | Comment RSSRSS comment feed

Use regular expressions to validate an email in c# using Regex

Sunday, 4 October 2009 18:32 by myro

If you need a simple method that validates an email address in c#, you are at the right place. The code snipped illustrated below, will check if the provided email address matches a regular expressionto and will determine if is a valid email or not.

public bool IsMailValid(string emailAddress)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"^(([^<>()[\]\\.,;:\s@\""]+");
    sb.Append(@"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@");
    sb.Append(@"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}");
    sb.Append(@"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+");
    sb.Append(@"[a-zA-Z]{2,}))$");
    Regex reStrict = new Regex(sb.ToString());
    bool isStrictMatch = reStrict.IsMatch(emailAddress);
    return isStrictMatch;
}

Use it, paste it and share it!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Set default button in Asp.Net pages and in asp:login form

Saturday, 3 October 2009 15:32 by myro

Setting the default botton that needs to be pushed when the Enter key is pressed into an ASPX page, can be implemented easly in Asp.Net.
Imagine that your page holds 2 controls:

  • a Asp.net TextBox named tb1
  • a Asp.net Button named btn1

To instruct the page, to point the default button  on btn1, you should use:

Page.Form.DefaultButton = btn1.ClientID;

Setting the focus on tb1 can be accomplished by:

Page.Form.DefaultFocus = tb1.ClientID;

As you can see, you have to use control's ClientID and not  the control's ID.

But what happens if you want to set the Page's DefaultButton property to a login button contained in a Login form control? Consider surrounding the <asp:Login /> control with a simple asp.net Panel:

<asp:Panel ID="Panel1" runat="server" Height="100%" Width="100%" DefaultButton="llogin$LoginButton">
    <asp:Login ID="llogin" runat="server"
         PasswordRecoveryUrl="~/passwordrecover.aspx"
         CreateUserUrl="~/register.aspx"
         TitleTextStyle-CssClass="contentGroupHeader"
         OnLoggedIn="SetCookie"  >
    <LoginButtonStyle CssClass="button" ></LoginButtonStyle>
    </asp:Login>
</asp:Panel>

...and  set the DefaultButton property using the syntax provided in the example.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   Web
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed