AspNetSource.com's Official BlogAspNetSource's Official Blog

Articles for ASP.NET, AJAX, SQL, C#, VB.NET, etc.


Top Articles

Dev-the-Web.com
Recent ASP.NET Messages:
to 14:
Hi, 1. Request.UserHostAddress // TO GET THE IP ADD 2. Request.UserHostName // TO GET THE MACHINE NAME
Wed, 01 Jul 2009 02:13:14 GMT

Ramez:
Hi all, How i get IP address of Machine or Machine Name . Any article or hint is apperciated best regards
Wed, 01 Jul 2009 00:45:19 GMT

Corwin:
I'm looking at the ASP code for my CMS, and looking for ways to optimize the script by removing redundant code and making existing code more efficient.
Tue, 09 Jun 2009 06:01:54 GMT

lancer:
Now heavily customizing a shopping cart for my personal use.
Tue, 09 Jun 2009 05:53:06 GMT

tihomir:
if you use Button's property PostBackUrl and you want when click to open url in a new window, add this property: OnClientClick = "window.document.forms[0].target = '_blank';"
Mon, 01 Jun 2009 03:24:40 GMT

tihomir:
I'm testing now the rss feed functionality :)
Sun, 31 May 2009 14:24:28 GMT

tihomir:
I'm reading now how to create web widgets using asp.net :)
Mon, 18 May 2009 07:19:45 GMT

tihomir:
New AJAX Toolkit release 3.5 SP1: LINK Three new controls: ComboBox, HTML Editor, Color Picker.
Sun, 17 May 2009 00:25:04 GMT

tihomir:
I wrote post about using MySQL in ASP.NET with many examples: LINK
Thu, 07 May 2009 10:23:17 GMT

tihomir:
again I'm adding new functionality for the site :D
Thu, 07 May 2009 09:51:26 GMT

Read More Messages >
 
<July 2009>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Categories

Links

Link to Us

(just copy-paste the text below in your page)

Author(s)

 
Posted by Tihomir Ivanov on 08 June 2009 17:26
Posted in: .NET Framework 

Here's a Table for categories of Security Classes in the System.Security.Cryptography Namespace:

Category Description
Encryption algorithms

This namespace includes the most important hashing and
encryption algorithms (ex. including DES, Triple DES,
TRIPLE_DES_3KEY, RC2, RC4, 128-bit RC4, DESX,
128-bit AES, 192-bit AES, and 256-bit AES) and classes
for creating digital signatures.

Helper classes When you need to create true cryptographic random numbers,
you can find helper classes in the
System.Security.Cryptography namespace. These classes
are for interacting with the underlying Windows cryptography
system (the CryptoAPI).
X509 certificates In System.Security.Cryptography.X509-Certificates namespace,
you can find all the necessary classes for working
with X509 certificates and (since .NET 2.0) classes for
accessing the Windows certificate store.
XML signature and encryption

You can find complete support of the XML signature and
encryption standards in the namespace System.Security.Cryptography.Xml.
The classes in this namespace are used for encrypting and signing
XML documents according to the standard published by the W3C.

CMS/PKCS#7 The .NET Framework (since .NET 2.0) has managed support for
creating CMS/PKCS-enveloped messages directly without
unmanaged calls. (CMS stands for Cryptographic Message
Syntax and PKCS stands for Public-Key Cryptography
Standard.)

Soon we will add more articles about Cryptography in .NET Framework :)

Posted by Tihomir Ivanov on 01 June 2009 07:18
Posted in: ASP.NET 

I've recently had the following issues:

I wanted to add 14 DataSources/GridViews (Each of them connects to different database) on a single page.

When I added them (using the standard way) the page began to loads very very slow. So, I decided:

1. the main content of the page (header, menu, footer, etc.) to load first

2. the DataSources/GridViews to become loading one by one.

Here's the solution of my issue:

1. Add user control for every page part:

I created ascx control for every page part. (the DataSources/GridViews were almost the same only differs by database name and table name, so I could use one control for all DataSources/GridViews).

ex.

<asp:Panel runat="server" ...>
   <asp:UpdatePanel runat="Server" ID="updateRSS" UpdateMode="Conditional">
     <contenttemplate>
        <asp:MultiView runat="server" ID="_multiView" ActiveViewIndex="0" >

          <asp:View runat="Server" ID="view1">
             Loading . . .
          </asp:View>

         <asp:View runat="server" ID="view2">
           <asp:GridView ID="_msgsGV" DataSourceID="_msgsDS" runat="server">
              . . .
           </asp:GridView>
           <asp:SqlDataSource ID="_msgsDS" runat="server">
             . . .
           </asp:SqlDataSource>
         </asp:View>

         <asp:View runat="Server" ID="view3">
           Unable to load content :(
         </asp:View>

        </asp:MultiView>

        <asp:Timer ID="_timerMessages" Interval="1" OnTick="OnTimerMessagesTick" runat="server" />
      </contenttemplate>
     </asp:UpdatePanel>
</asp:Panel>

We have to put all control's content in a separate UpdatePanel. To show loading, GridView/SqlDataSource, 'unable to load page' I use Views in a MultiView.

We need asp:Timer too. in the OnTick event handler will be the real filling of the GridView/SqlDataSource.

2. Handle the OnTick event:

protected void OnTimerMessagesTick(object s, EventArgs e)
{
     try
    {
         //t Display the loaded GridView/SqlDataSource
         _mv.ActiveViewIndex = 1;
    }
    catch (Exception)
   {
       //t Diplay the "unable to load"
       _mv.ActiveViewIndex = 2;
    }

    //Important!
    _timerMessages.Enabled = false;
}

In the OnTick event we do two things:

First, change the _mv' ActiveViewIndex to show the GridView/SqlDataSource.

and second (very important) Disable the asp:Timer, we do it to disable another GridView/SqlDataSource reloading.

3. Adding the ascx control to a aspx page:

<%@ Page Language="C#" . . . %>
. . .
<%@ Register Src="~/controls_path/ourControl.ascx" TagName="our_cotrol" TagPrefix="usr" %>

 

<asp:ScriptManager . . . />

 . . .

<script type="text/javascript">
   function scrollTo()
  {
      return;
  }
</script>

. . .

<usr:our_cotrol ID="some_id" . . . ></usr:our_cotrol>

We add the ascx control in the standard way.

NOTE: Before some of the timer fires, sometimes the user "scrolled down" the page and then the Ajax Timer fires and the "scroll position" of the page is changed again to the top.

So, we over-ride the javascript function that is called by adding the code:

<script type="text/javascript">
   function scrollTo()
  {
      return;
  }
</script>

You can see the working example of the article HERE

That's all :)

Posted by Tihomir Ivanov on 07 May 2009 04:37
Posted in: ASP.NET SQL 

It may sound strange but using MySql database in ASP.NET is really easy. I spend few days trying to create ASP.NET site that uses
MySql DataBase and in this post I'll try to explain all points for that, also how to use MySql in SqlDataSources and GridViews.

I. You need to download MySql.Data.dll assembly and add reference to it. LINK

1. download sources and open the project

2. recompile the MySql.Data.Dll to allow for partially trusted callers. This can be done by adding the following to the AssemblyInfo.cs file.

[assembly:AllowPartiallyTrustedCallers]

3. copy-paste the recompiled in your project's bin folder

4. add a reference to it.

Note*: if you don't have problems using partially trusted callers you don't need to recompile the assembly.

Note*: some hosting providers supports it in the GAC, you'd better check before doing steps above.

II. Configure The Web.Config File

For ex. in the MySql DataBase, let's we have:

Server' URL: SERVER_URL

DataBase' Name: DATABASE_NAME

DataBase' Username: DB_USER_ID

DataBase' Password: DB_USER_PASSWORD

now, in the Web.Config file add:

1. <connectionStrings>
       ...
       <add name="YOUR_CONN_STRING_NAME" connectionString="Server=SERVER_URL;Database=DATABASE_NAME;Uid=DB_USER_ID;Pwd=DB_USER_PASSWORD;"/>
      ...
    </connectionStrings>

2. <compilation ...>
     ...
    <add assembly="MySql.Data, Version=VERSION_OF_THE_ASSEMBLY, Culture=neutral, PublicKeyToken=ASSEMBLY_PUBLIC_KEY" />
    ...
    </compilation>

   You can check VERSION_OF_THE_ASSEMBLY and ASSEMBLY_PUBLIC_KE from the MySql.Data.dll's source file: AssemblyInfo.cs

3. <pages ... >
     <controls>
       <add ... />
       ...
     </controls>
    <namespaces>
     <add namespace="MySql.Data"/>
     <add namespace="MySql.Data.MySqlClient"/>
    </namespaces>
  </pages>

4. <system.data>
     <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory,         MySql.Data,Version=VERSION_OF_THE_ASSEMBLY,Culture=neutral"/>
     </DbProviderFactories>
   </system.data>

III. Using MySql in your code (C# examples)

For ex. in the MySql DataBase DATABASE_NAME we have a table with name TABLE_NAME (ID, FIELD1)

1. INSERT INTO ... VALUES ...:

using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;

 ...

MySqlConnection msqlConnection;
string sql;
MySqlCommand msqlCommand;

try
{
    msqlConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings[YOUR_CONN_STRING_NAME].ConnectionString);

    sql = "INSERT INTO TABLE_NAME (FIELD_1) VALUES (?param1);";

    msqlCommand = new MySqlCommand(sql, msqlConnection);

    msqlCommand.Parameters.AddWithValue("?param1", SOME_LOCAL_VARIABLE);

    try
   {
      msqlConnection.Open();

      msqlCommand.ExecuteNonQuery();
   }
   finally
  {
     msqlConnection.Close();
   }
}
catch (Exception ex)
{
    // handle the exception
    ...
}

Note*: for parameters name we use ? instead of @

2. SELECT ... FROM ... WHERE:

using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;

 ...

MySqlConnection msqlConnection;
MySqlCommand msqlCommand;
MySqlDataReader msqlDataReader;
string sql;

try
{
     msqlConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings[YOUR_CONN_STRING_NAME].ConnectionString);

     sql = "SELECT FIELD_1 FROM TABLE_NAME WHERE ID = ?param1;";

     msqlCommand = new MySqlCommand(sql, msqlConnection);

     msqlCommand.Parameters.AddWithValue("?param1", SOME_LOCAL_VARIABLE);

     try
    {
       msqlConnection.Open();

      msqlDataReader = msqlCommand.ExecuteReader();

      if (msqlDataReader.Read())
         result = (msqlDataReader).GetString("message");
     }
     finally
    {
       msqlConnection.Close();
    }
}
catch (Exception ex)
{
   // handle the exception
    ...
}

3. SELECT COUNT(*):

string result = "";
MySqlConnection msqlConnection;
MySqlCommand msqlCommand;
string sql;
object obj;

try
{
    msqlConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings[YOUR_CONN_STRING_NAME].ConnectionString);

    sql = "SELECT COUNT(*) FROM TABLE_NAME WHERE YEAR(created) = ?param1;";

    msqlCommand = new MySqlCommand(sql, msqlConnection);

    msqlCommand.Parameters.AddWithValue("?param1", SOME_LOCAL_VARIABLE);

    try
   {
      msqlConnection.Open();

     obj = msqlCommand.ExecuteScalar();

     if (obj != null)
      result = obj.ToString();
   }
   finally
  {
     msqlConnection.Close();
   }
 }
catch (Exception ex)
{
  // handle the exception
    ...
}

IV. Using MySql in aspx pages (SqlDataSource and GridView example)

<asp:GridView ID="_gridViewId" DataSourceID="_dataSourceId" runat="server"
 AutoGenerateColumns="false" DataKeyNames="ID" AllowPaging="true" PageSize="100">
   <Columns>
     <asp:BoundField DataField="FIELD_1" HeaderText="SOME_HEADER_TEXT" />
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="_dataSourceId" runat="server"
   ConnectionString="<%$ ConnectionStrings:YOUR_CONN_STRING_NAME %>"
   ProviderName="MySql.Data.MySqlClient"
   SelectCommand="SELECT * FROM TABLE_NAME">
</asp:SqlDataSource>

Well, That's all. I hope that this post'll be usefull to someone else, too :)

 

 

 

12345678910...


 
AspNetSource.com


 
Our Sponsors:  Asp.net file upload component  |   Flash file uploader