RSS

Monthly Archives: November 2013

Encryption/Decryption Function in .NET


 

C#

 

public string Encrypt(string str)
    {
        string EncrptKey = “2013;[pnuLIT)X”;
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }

 public string Decrypt(string str)
    {
        str = str.Replace(” “, “+”);
        string DecryptKey = “2013;[pnuLIT)Y”;
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byte[] inputByteArray = new byte[str.Length];
        byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        inputByteArray = Convert.FromBase64String(str.Replace(” “, “+”));
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        System.Text.Encoding encoding = System.Text.Encoding.UTF8;
        return encoding.GetString(ms.ToArray());
    }

 

VB.Net

Public Function Encrypt(str As String) As String
Dim EncrptKey As String = “2013;[pnuLIT)X”
Dim byKey As Byte() = {}
Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _
205, 239}
byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8))
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray As Byte() = Encoding.UTF8.GetBytes(str)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
End Function

 
Public Function Decrypt(str As String) As String
str = str.Replace(” “, “+”)
Dim DecryptKey As String = “2013;[pnuLIT)Y”
Dim byKey As Byte() = {}
Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _
205, 239}
Dim inputByteArray As Byte() = New Byte(str.Length – 1) {}

byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8))
Dim des As New DESCryptoServiceProvider()
inputByteArray = Convert.FromBase64String(str.Replace(” “, “+”))
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
End Function

 

Advertisements
 
1 Comment

Posted by on November 22, 2013 in ASP Dot Net C#, Vb.Net

 

Tags: , , ,

Access crystal report’s print button programmatically


You can print crystal report directly without open it / use crystal report print button

you can print report from Button click Event. Call this Method on Click Event.Below line opens up print dialog box to print without showing print preview

CrystalReport.PrintToPrinter()

Below line directly sends reportdocument to default printer.

CrystalReport.PrintToPrinter(1,true,0,0);

also you can set paper size Dynamically

CrystalReport.PrintOptions.PaperSize = PaperSize.PaperA4

 

 
Leave a comment

Posted by on November 20, 2013 in Crystal Report

 

Tags: ,

How to limit export formats in crystal reports


In Crystal Report Export Format are various Types . so , if you want to show limited export format then use this following code.

C#::

int exportFormatFlags = (int)(CrystalDecisions.Shared.ViewerExportFormats.PdfFormat | CrystalDecisions.Shared.ViewerExportFormats.ExcelFormat );
crystalReportViewer1.AllowedExportFormats = exportFormatFlags;

crystalReportViewer1.ReportSource = Report;

crystalReportViewer1.Refresh();

 

VB.Net

 Dim formats As Integer

    formats = (CrystalDecisions.Shared.ViewerExportFormats.PdfFormat Or CrystalDecisions.Shared.ViewerExportFormats.XLSXFormat)

    CrystalReportViewer1.AllowedExportFormats = formats


http://stackoverflow.com/questions/5152018/how-to-limit-export-formats-in-crystal-reports
 
Leave a comment

Posted by on November 20, 2013 in Crystal Report

 

Tags:

Change “Main Report” tab in Crystal Report


You Can  Change the “Main Report”  tab/title to be removed in the ReportViewer.  Or you can the title be changed to it.

VB.Net 

You Can Call following function after form.show method(where form conatin crystalReportViewer Control). At the end of datasource like

//////////////////

Report.SetDataSource(ds1)
CrystalReportViewer1.ReportSource = Report
CrystalReportViewer1.Refresh()
FormatReportViewer()

/////////////////////////////////////////////Function/Method/Recommande////////////////

Private Sub FormatReportViewer()

Dim thisObj As Object

Dim MyPageView As CrystalDecisions.Windows.Forms.PageView

Dim tcontrol As Windows.Forms.TabControl

 

For Each thisObj In CrystalReportViewer1.Controls

 

Select Case UCase(thisObj.GetType.Name)

‘Case “STATUSBAR”

‘    CType(thisObj, StatusBar).Visible = False

Case “PAGEVIEW”

MyPageView = CType(thisObj, CrystalDecisions.Windows.Forms.PageView)

tcontrol = CType(MyPageView.Controls(0), TabControl)

With tcontrol

 

 

If .TabPages.Count > 0 Then

 

With .TabPages(0)

 

.Text = “Your Heading”

.Visible = True

End With

End If

End With

End Select

Next

 

End Sub

 

Other Way.

I know this is very old post, but I have found a way to hide the “Main Report” tab so I want to share it.  This seems to still be an issue in Win forms even with version 13.

 

foreach ( object control in _crystalReportViewer.Controls )

{

string name = control.GetType().Name;

if ( name == “PageView” )

{

CrystalDecisions.Windows.Forms.PageView pageView = control as CrystalDecisions.Windows.Forms.PageView;

if ( pageView != null && pageView.Controls.Count > 0 )

{

TabControl tabControl = pageView.Controls[0] as TabControl;

if ( tabControl != null && tabControl.TabPages.Count > 0 )

{

TabPage tabPage = tabControl.TabPages[0];

 

tabControl.Top = tabControl.Top – tabControl.ItemSize.Height;

tabControl.Height = tabControl.Height + tabControl.ItemSize.Height;

tabControl.Region = new Region( new RectangleF( tabPage.Left, tabPage.Top, tabPage.Width, tabPage.Height + tabControl.ItemSize.Height ) );

}

}

break;

}

}

 

 

I call this code from the load event handler.

 

Tab Print

 
Leave a comment

Posted by on November 20, 2013 in Crystal Report

 

Tags: , , ,

microsoft sql server error 5120 access denied


Use the below script to find the sql service account

 

declare @sqlser varchar(20)

EXEC master..xp_regread @rootkey=‘HKEY_LOCAL_MACHINE’,@key=‘SYSTEM\CurrentControlSet\Services\MSSQLSERVER’,

@value_name=‘objectname’, @value=@sqlser OUTPUT

PRINT ‘Account Starting SQL Server Service:’ +convert(varchar(30),@sqlser)

After getting the service account try the below steps to provide privilege

 

  • Right click on the folder where your database file like (Testdb.mdf & Test.ldf) and click on properties
  • Click on security tab
  • Click on Add button and add sql service account
  • Provide modify privilege and click ok
  • Verify both mdf and ldf have modify privilege
  • Give full control againt your system login user
  • Attach the db!
 
Leave a comment

Posted by on November 14, 2013 in SQL Query

 

Tags: , , , ,