Wednesday, September 30, 2009

Store PDF in sql server database in binary formate

Using this code we can upload pdf file to sql database table in binary type field.


using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public class EmployeeData
{
public static void Main()
{
DateTime hireDate = DateTime.Parse("5/21/99");
AddEmployee("Jones", "Mary", "Sales Representative", hireDate, 5, "jones.bmp");
}

public static void AddEmployee(string lastName, string firstName, string title, DateTime hireDate , int reportsTo, string photoFilePath)
{
byte[] photo = GetPhoto(photoFilePath);

SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");

SqlCommand addEmp = new SqlCommand("INSERT INTO Employees (LastName, FirstName, Title, HireDate, ReportsTo, Photo) " +
"Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)", nwindConn);

addEmp.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = lastName;
addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName;
addEmp.Parameters.Add("@Title", SqlDbType.NVarChar, 30).Value = title;
addEmp.Parameters.Add("@HireDate", SqlDbType.DateTime).Value = hireDate;
addEmp.Parameters.Add("@ReportsTo", SqlDbType.Int).Value = reportsTo;

addEmp.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo;

nwindConn.Open();

addEmp.ExecuteNonQuery();

nwindConn.Close();
}

public static byte[] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

byte[] photo = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

return photo;
}
}

Get all IP addresses on LAN

this is a simple vb.net code to retrieve all ip addresses on your local network.
remember to import System.Net.


Public Shared Function GetAllIPADDRESSES(Optional ByVal args As String() = Nothing) As Integer
'args in the signature is optional, without it
'the function will simply get the hostname
'of the local machine then go from there

Dim strHostName As New String("")
If args.Length = 0 Then
' Getting Ip address of local machine...
' First get the host name of local machine.
strHostName = DNS.GetHostName()
Console.WriteLine("Local Machine's Host Name: " + strHostName)
Else
strHostName = args(0)
End If

' Then using host name, get the IP address list..
Dim ipEntry As IPHostEntry = DNS.GetHostByName(strHostName)
Dim addr As IPAddress() = ipEntry.AddressList

Dim i As Integer = 0
While i < addr.Length
Console.WriteLine("IP Address {0}: {1} ", i, addr(i).ToString())
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return 0
End Function

Tuesday, September 22, 2009

Crystal Report Example in asp.net

This code shows how to create Reports using ASP.NET Crystal Reports


Imports System
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\vb.net projects\dept_mstr.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = ConnString

Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim sql As String = "select distinct dept_no,dept_name from DEPARTMENT"
myConnection.Open()

Dim com As New OleDbCommand
Dim dbread
com = New OleDbCommand(sql, myConnection)
dbread = com.ExecuteReader
While dbread.read
DropDownList1.Items.Add(dbread.getVALUE(0))
DropDownList2.Items.Add(dbread.getstring(1))
End While

End Sub

Protected Sub CrystalReportViewer1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Init

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Fltr
CrystalReportViewer1.ReportSource = "E:\WebSite2\crystalreport1.rpt"
Fltr = "{DEPARTMENT.dept_no}='" & Me.DropDownList1.Text & "' and '" & Me.DropDownList2.Text & "'"
CrystalReportViewer1.SelectionFormula = (Fltr)
End Sub
End Class

Use of Culture in asp.net

This sample code shows use of culture in asp.net


Design:

<form id="form1" runat="server">
<div>
Cultures: <asp:DropDownList id="ddlCulture" DataTextField="DisplayName"
DataValueField="Name" DataSourceID="GetCultureInfo" Runat="server" />
<asp:ObjectDataSource id="GetCultureInfo" TypeName="System.Globalization.CultureInfo"
SelectMethod="GetCultures" Runat="server">
<SelectParameters>
<asp:Parameter Name="types" DefaultValue="SpecificCultures" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Button id="btnSelect" Text="Select" Runat="server" onclick="btnSelect_Click"></asp:Button><br />
Date:<asp:Label id="lblDate" Runat="server" /><br />
Price : <asp:Label id="lblPrice" Runat="server" />
</div>
</form>



Web.Config:

<profile>
<properties>
<add
name="UserCulture"
defaultValue="en-US" />
<add
name="UserUICulture"
defaultValue="en" />
</properties>
</profile>


Code Behind:

protected override void InitializeCulture()
{
Culture = Profile.UserCulture;
UICulture = Profile.UserUICulture;
}

protected void btnSelect_Click(object sender, EventArgs e)
{
Profile.UserCulture = ddlCulture.SelectedValue;
Profile.UserUICulture = ddlCulture.SelectedValue;
//Response.Redirect(Request.Path);
}

void Page_PreRender()
{
lblDate.Text = DateTime.Now.ToString("D");
lblPrice.Text = (512.33m).ToString("c");
}