Tuesday, February 10, 2009

Encryption and Decription


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
using System.Xml;using System.Text;

using System.Security.Cryptography;

using System.IO;using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string str= Encrypt("Silpa");

SqlConnection con = new SqlConnection("Data Source=INFO-9;Initial Catalog=test;Integrated Security=True");

SqlCommand cmd=new SqlCommand("insert into pwdd values('"+str+"')",con);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

string str1 = Decrypt("88tlfz/eGbtHNKDnXU6gQw==");

}
public static string Decrypt(string TextToBeDecrypted)

{

RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";

string DecryptedData;
try {

byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());

//Making of the key for decryption

PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

//Creates a symmetric Rijndael decryptor object.

ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);

//Defines the cryptographics stream for decryption.THe stream contains decrpted data

CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] PlainText = new byte[EncryptedData.Length];

int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); memoryStream.Close();

cryptoStream.Close();
//Converting to string

DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);

}

catch

{

DecryptedData = TextToBeDecrypted;

}

return DecryptedData;

}
public static string Encrypt(string TextToBeEncrypted)

{

RijndaelManaged RijndaelCipher = new RijndaelManaged();

string Password = "CSC";

byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);

byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

//Creates a symmetric encryptor object.

ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

MemoryStream memoryStream = new MemoryStream();

//Defines a stream that links data streams to cryptographic transformations CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);

cryptoStream.Write(PlainText, 0, PlainText.Length);

//Writes the final state and clears the buffer

cryptoStream.FlushFinalBlock();

byte[] CipherBytes = memoryStream.ToArray();

memoryStream.Close();

cryptoStream.Close();

string EncryptedData = Convert.ToBase64String(CipherBytes);
return EncryptedData;

}
}

Wednesday, January 14, 2009

BulletedList Example in Web Application




HTML Code:

OutPut:


C# Source Code:

protected void Page_Load(object sender, EventArgs e)
{
if(! Page.IsPostBack)
Label1.Text = "Page is loaded first time.";
else
Label1.Text = "PostBack operation performed by clicking the link button";

}

Adding Items in ListBox

OutPut:



















HTML Code:



C# Source Code:

int HiddenValue;

protected void Button1_Click(object sender, EventArgs e)
{
HiddenValue = Convert.ToInt32(HiddenField1.Value.ToString());
ListBox1.Items.Add("Item " + HiddenValue.ToString());
Label4.Text = HiddenValue.ToString();
HiddenValue = HiddenValue + 1;
HiddenField1.Value = HiddenValue.ToString();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = ListBox1.SelectedItem.Text.ToString();
}

TextBox Example in Web Application

OutPut:





HTML Code:







C# Code:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "You have clicked the button";
TextBox4.Text = "This is a multi line textbox. This is a multi line textbox. This is a multi line textbox. This is a multi line textbox. ";

TextBox3.Text = TextBox2.Text;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox1.Text = "The Text is changed.";

}

Monday, January 12, 2009

MessageBox in Web Application

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

public partial class msgbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string message = "Do you want to take print?";
string caption = "Maximum Three Prints";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxDefaultButton btn = MessageBoxDefaultButton.Button1;
DialogResult result;
result = MessageBox.Show(message, caption, buttons,icon,btn);
if (result == DialogResult.Yes)
{

Label1.Text = "success";

}
else
{
Label1.Text = "Failure";
}

}
}

Saturday, December 27, 2008

Data on datagrid from excel sheet

Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\excel\\excel\\Sample.xls;" + "Extended Properties=Excel 8.0;";

OleDbConnection connection = new OleDbConnection(strConn);

OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);

DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
dataGrid1.DataSource = myDataSet.Tables[0].DefaultView;



}
}
}

OutPut is:

Friday, October 10, 2008

VB Tutorials

Database connection with MSAcess Database and insert,update,delete,select commands in VB

Output is:



Source Code is:

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub Combo1_Click()
rs.Open "select * from punch where Card_id='" & Combo1.Text & "' ", con, adOpenDynamic, adLockBatchOptimistic
While rs.EOF = False
Text1.Text = rs(0)
Text2.Text = rs(1)

rs.MoveNext
Wend
rs.Close
End Sub

Private Sub Command1_Click()
rs.Open "select * from punch where Card_id='12'", con, adOpenDynamic, adLockBatchOptimistic
While rs.EOF = False
Text1.Text = rs(0)
Text2.Text = rs(1)
rs.MoveNext
Wend
rs.Close
End Sub

Private Sub Command2_Click()
con.Execute "insert into punch values('" & Text1.Text & "','" & Text2.Text & "')"
MsgBox "inserted"
End Sub

Private Sub Command3_Click()
con.Execute "update punch set User_Name='" & Text2.Text & "' where Card_id='" & Text1.Text & "'"
MsgBox "updated"

End Sub

Private Sub Command4_Click()
con.Execute "delete from punch where Card_id='" & Text1.Text & "'"
MsgBox "deleted"

End Sub

Private Sub Form_Load()
con.ConnectionString = "provider=microsoft.jet.oledb.4.0;" & " data source=" & App.Path & "\pu.mdb"
con.Open
rs.Open "select Card_id from punch", con, adOpenDynamic, adLockBatchOptimistic
While rs.EOF = False
Combo1.AddItem rs(0)
rs.MoveNext
Wend
rs.Close

End Sub

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...