Haz 27 2009

[C#] Use a key string as an array index - Dictionary, SortedDictionary, Hashtable classes

Category: C#Latif @ 12:39

 

Hi,

   I will try to explain the differences between Dictionary, SortedDictionary, Hashtable and

at the same time,we will answer how to use a key string as an array index?

 

Namespace:  System.Collections.Generic

Dictionary(TKey, TValue) Class

SortedDictionary (TKey, TValue) Class

 

Namespace:  System.Collections

Hashtable

 

Code with explanation:

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Collections;

 

namespace genericTypes2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            //System.Collections.Generic.Dictionary

 

            Dictionary<string, string> dictionaryNew = new Dictionary<string, string>();

            //Use Add() to assign value

            dictionaryNew.Add("company", "TRIODOR");

            dictionaryNew.Add("grup", "MD INFO");

            dictionaryNew.Add("latif", "ozturk");

            dictionaryNew.Add("recep", "guzel");

            dictionaryNew.Add("talha", "turan");

            dictionaryNew.Add("pinar", "alasli");

            dictionaryNew.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNew["volkan"] = "cakar";

            dictionaryNew["bugra"] = "postaci";

            dictionaryNew["Erkan"] = "BALABAN";

 

            listBox7.Items.Add("DICTIONARY <>");

            listBox7.Items.Add(" ");

 

            //to list dictionaryNew in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNew)

            {

                listBox7.Items.Add(obj.Key + " " + obj.Value);

                //other way of getting value

                //listBox7.Items.Add(dictionaryNew["" + obj.Key + ""]);

            }

 

            //System.Collections.Generic.SortedDictionary

 

            SortedDictionary<string, string> dictionaryNewSorted = new SortedDictionary<string, string>();

            //Use Add() to assign value

            dictionaryNewSorted.Add("company", "TRIODOR");

            dictionaryNewSorted.Add("grup", "MD INFO");

            dictionaryNewSorted.Add("latif", "ozturk");

            dictionaryNewSorted.Add("recep", "guzel");

            dictionaryNewSorted.Add("talha", "turan");

            dictionaryNewSorted.Add("pinar", "alasli");

            dictionaryNewSorted.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNewSorted["volkan"] = "cakar";

            dictionaryNewSorted["bugra"] = "postaci";

            dictionaryNewSorted["Erkan"] = "BALABAN";

 

            listBox8.Items.Add("SORTED DICTIONARY<>");

            listBox8.Items.Add(" ");

 

 

            //to list dictionaryNewSorted in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNewSorted)

            {

                listBox8.Items.Add(obj.Key + " " + obj.Value);

            }

 

 

            //System.Collections.Hashtable

 

            Hashtable hashTableNew = new Hashtable();

            //Use Add() to assign value

            hashTableNew.Add("company", "TRIODOR");

            hashTableNew.Add("grup", "MD INFO");

            hashTableNew.Add("latif", "ozturk");

            hashTableNew.Add("recep", "guzel");

            hashTableNew.Add("talha", "turan");

            hashTableNew.Add("pinar", "alasli");

            hashTableNew.Add("elif", "onder");

            //You can assign value like that as well

            hashTableNew["volkan"] = "cakar";

            hashTableNew["bugra"] = "postaci";

            hashTableNew["Erkan"] = "BALABAN";

 

            listBox9.Items.Add("HASH TABLE<>");

            listBox9.Items.Add(" ");

           

            //to list hashTableNew in the ListBox - use DictionaryEntry

            foreach (DictionaryEntry obj in hashTableNew)

            {

                listBox9.Items.Add(obj.Key + " " + obj.Value);

            }

 

        }

    }

}

 

 

 

It is easy to see the differences between Dictionary, SortedDictionary, Hashtable in the picture above.

 

Dictionary -> uses FIFO

SortedDictionary -> sorting by array index

Hashtable -> sorting by hash algorithm

 

And also don’t forget that Dictionary is faster than others.

 

See you...

 

Tags: , , , ,

Haz 19 2009

Call Crystal report from ASP.Net & Changing connectionInfo of Crystal report at runtime

Category: ASP.NET | Crystal ReportLatif @ 17:57

 Code with descriptions: file.cs

    try

    {

        //Get params from QueryString

        string startdate = context.Request.QueryString["StartDate"];

        string enddate = context.Request.QueryString["EndDate"];

        //Define report format

        context.Response.ContentType = "application/pdf";

 

        ReportDocument crReport = new ReportDocument();

        crReport.Load(context.Server.MapPath("rpRevenueSharingAll.rpt"));

        //Set the parameters

  crReport.SetParameterValue(0, startdate); //STARTDATE

        crReport.SetParameterValue(1, enddate);//ENDDATE

       

        //Retrieve Web.config's connection string information

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);

        string password = builder.Password;

        string username = builder.UserID;

        string dataSource = builder.DataSource;

        string initialCatalog = builder.InitialCatalog;

 

        //Define new connection for Crystal Report

        CrystalDecisions.Shared.ConnectionInfo connectionInfo = new CrystalDecisions.Shared.ConnectionInfo();

        connectionInfo.DatabaseName = initialCatalog;

        connectionInfo.UserID = username;

        connectionInfo.Password = password;

        connectionInfo.ServerName = dataSource;

 

        // set report connection for main report

        SetDBLogonForReport(connectionInfo, crReport);

        // set report connection for any subreports

        SetDBLogonForSubreports(connectionInfo, crReport);

 

        MemoryStream ms = (MemoryStream)crReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

        context.Response.BinaryWrite(ms.ToArray());

 

    }

    catch (System.Threading.ThreadAbortException ex)

    {

       

    }

    finally

    {

        context.Response.End();

    }

 You can see the functions I have used above. I found the functions from http://www.crystalreportsbook.com/Forum/forum_posts.asp?TID=1249&PID=4186

        private void SetDBLogonForReport

          (CrystalDecisions.Shared.ConnectionInfo connectionInfo,

            ReportDocument reportDocument)

        {

 

            Tables tables = reportDocument.Database.Tables;

            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)

            {

                TableLogOnInfo tableLogonInfo = table.LogOnInfo;

                tableLogonInfo.ConnectionInfo = connectionInfo;

                table.ApplyLogOnInfo(tableLogonInfo);

            }

        }

 

        private void SetDBLogonForSubreports

          (CrystalDecisions.Shared.ConnectionInfo connectionInfo,

           ReportDocument reportDocument)

        {

            Sections sections = reportDocument.ReportDefinition.Sections;

            foreach (Section section in sections)

            {

                ReportObjects reportObjects = section.ReportObjects;

                foreach (ReportObject reportObject in reportObjects)

                {

                    if (reportObject.Kind == ReportObjectKind.SubreportObject)

                    {

                        SubreportObject subreportObject = (SubreportObject)reportObject;

                        ReportDocument subReportDocument =

                          subreportObject.OpenSubreport(subreportObject.SubreportName);

                        SetDBLogonForReport(connectionInfo, subReportDocument);

                    }

                }

            }

        }

 

Tags: ,

Haz 18 2009

Retrieve individual data from ConnectionString

Category: ASP.NETLatif @ 18:04

   If you want to retrieve data(password ,UserID,DataSource,InitialCatalog) , you can use SqlConnectionStringBuilder (From .NET Framework 2.0, there is System.Data.Common.DbConnectionStringBuilder  and its derived classes (SqlConnectionStringBuilder for SqlServer))

string connectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);

string password = builder.Password;
string username = builder.UserID;
string dataSource = builder.DataSource;
string initialCatalog = builder.InitialCatalog;

 

Tags: ,

Haz 12 2009

LINQ: Max

Category: ASP.NET | LINQLatif @ 16:47

LINQ da SQL deki max islemini nasil mi yapariz?

Asagidaki sekilde,

MDDBDataContext dc = new MDDBDataContext();

var lastNumber = (from r in dc.RevenueSharings
                                  select (int?)r.StatementNumber).Max();

 

SQL server profiler tarafinda asagidaki sekilde goreceksiniz...

SELECT MAX([t0].[StatementNumber]) AS [value]
FROM [dbo].[RevenueSharing] AS [t0]

Tags: , , ,

Haz 11 2009

Problem with Request.Querysting and Plus Sybmol (+)

Category: ASP.NETLatif @ 08:00

Merhaba,

Eger GET methodu ile URL den parametre geciriyorsaniz ve parametrenizde arti(+) isareti geciyor ise ASP.NET sayfaniz da bu parametreyi alirken arti(+) isaretinin boslug'a donustugunu goreceksiniz.

Ornegin,

http://localhost/mdweekly/zoekresultaten.aspx?source=Speelgoed-+-Hobby 

Seklinde bir URL iniz varsa

Sayfanizdan Request.QueryString["source"] seklinde almak istediginiz degerin "Speelgoed- -Hobby" seklin de geldigini farkedeceksiniz.

Bunun onune gecmek icin parametreyi asagidaki sekilde alabilirsiniz.

source = System.Web.HttpUtility.UrlEncode(Request.QueryString["source"]);

 

Tags: