Friday, 15 March 2013

Storing and Retrieving From App.config

 <?xml version="1.0"?>
<configuration>
  <configSections>

    <sectionGroup name="Country">
        <section name="CountryMapping" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>

<connectionStrings>
    <clear />
    <add name="XYZ" connectionString="Data Source=localhost;Initial Catalog=NorthWind; Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

<Country>
    <CountryMapping>

      <add key="United States of America" value="USA"/>
      <add key="Kingdom of Great Britain" value="UK"/>
      <add key="China" value="CH"/>

    </CountryMapping>
  </Country>

....
</configuration>

C# Code:
using System.Collections.Specialized;

 NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("Country/CountryMapping");
foreach (String valueCollection in section)
{
  if (dr["Country"].ToString() == valueCollection)
  {
     dr["Country"] = section[valueCollection];
  }
} 


For more reference:
http://www.codeproject.com/Articles/21036/Working-with-Configuration-Files-app-config-in-C-C

Sunday, 3 March 2013

Convert Datareader to Dataset C#


public static DataSet convertDataReaderToDataSet(SqlDataReader 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"]; //+ "<C" + i + "/>";
                        // 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;
        }

Wednesday, 14 November 2012

SSRS:Export to Excel in Individual tabs

 ' Returns a properly formatted list of comma seperated marks
    ' Assumes that the input list is a list of comma seperated values
    ' Adds single quotes around each value and then joins them back together
    Public Function FormattedCommaSeperatedList(ByVal pCommaSeparatedString As String) As String       
        Dim sTokens As String() = pCommaSeparatedString.Split(",")
        Dim iTokenCount As Integer = 0
        FormattedCommaSeperatedList = ""
        If sTokens.Length > 1 Then
            Dim sToken As String
            For Each sToken In sTokens
                'for each token, concat with the single quote and put back into output string
                FormattedCommaSeperatedList = FormattedCommaSeperatedList + "'" + Trim(sToken) + "'"

                If iTokenCount < sTokens.Length - 1 Then
                    FormattedCommaSeperatedList = FormattedCommaSeperatedList + ","
                End If
                iTokenCount = iTokenCount + 1
            Next

        Else
            FormattedCommaSeperatedList = FormattedCommaSeperatedList + "'" + sTokens(0) + "'"
        End If
    End Function

Tuesday, 28 February 2012

Add .svc in MIME type IIS7

1. Add application in IIS7
2. Right click+switch to Features view
3. Double Click on MIME type
4. From Actions--->  click Add











5. .svc and application/octetstream + Click OK
OR
(try from your side.)
1.) a. Cd %systemRoot%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\
b. ServiceModelReg.exe -i
c. ServiceModelReg.exe -r

Thursday, 5 January 2012

Procedure with output parameter

With the help of Enterprise library:
Solution:

 DbCommand cmdDel = _db.GetStoredProcCommand("USP_YOURPROCEDURE");
                _db.AddOutParameter(cmdDel, "@STATUS", DbType.String, int.MaxValue);
                _db.AddInParameter(cmdDel, "@VID", DbType.Int32, HFVID.Value);
                _db.ExecuteDataSet(cmdDel);
               
                lblStatus.Text = cmdDel.Parameters["@STATUS"].Value.ToString();

A name was started with an invalid character. Error processing resource 'http://localhost/WCF...'


<>The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

A name was started with an invalid character. Error processing resource 'http://localhost/WCFIISService/Service.svc'. Line...
<%@ ServiceHost Service="WCFDefault.Service1" %>

Solution:

IN IIS>Handler Mappings>Add Managed Handlers

Request-path
*.svc

Type:
System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Name
svc-integrated

Friday, 9 December 2011

Opening Outlook with new e-mail from Asp.net

add Outlook reference through com
then include  " using Outlook = Microsoft.Office.Interop.Outlook;  " in header
use the following code for opening outlook with new email

code:

try
        {
            // Create the Outlook application.
            Outlook.Application oApp = new Outlook.Application();

            // Get the NameSpace and Logon information.
            Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

            // Log on by using a dialog box to choose the profile.
            oNS.Logon("Outlook", "", true, true);

            // Alternate logon method that uses a specific profile.
            // TODO: If you use this logon method,
            //  change the profile name to an appropriate value.
            //oNS.Logon("YourValidProfile", Missing.Value, false, true);

            // Create a new mail item.
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

            // Set the subject.
            oMsg.Subject = "Testing";

            // Set HTMLBody.
            String sHtml;
            sHtml = "<HTML>\n" +
               "<HEAD>\n" +
               "<TITLE>Sample GIF</TITLE>\n" +
               "</HEAD>\n" +
               "<BODY><P>\n" +
               "<h1><Font Color=Green>Testing</Font></h1></P>\n" +
               "<P>Testing Testing Testing Testing</P>\n" +
               "<P>Testing Testing Testing Testing</P>\n" +
               "</BODY>\n" +
               "</HTML>";
            oMsg.HTMLBody = sHtml;
          //  oMsg.Attachments.Add(OpenFile(@"C:\MyAttachmentFi.txt"), Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
            //try for attachments sorry the above code is not working..
           

            // Add a recipient.
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            // TODO: Change the recipient in the next line if necessary.
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("emailid@mail.com");
           
         oRecip.Resolve();
           oMsg.Display(oMsg);  //code for display the outlook
            // Send.
      //    oMsg.Send();    //code for sending

            // Log off.
          oNS.Logoff();

            // Clean up.
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oNS = null;
            oApp = null; 
        }

         // Simple error handling.
        catch (Exception ess)
        {
            Console.WriteLine("{0} Exception caught.", ess);
        } 


screen shot:


Thankyou..:)