Keep in touch

you can keep in touch with my all blogs and sites by install this Toolbar...
http://rworldtoolbar.ourtoolbar.com/

Saturday, September 27, 2008

GenerateRandomName for images in asp.net 2.0 using c#

public string GenerateRandomName()
{

string allowedChars = "";
allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
allowedChars += "1,2,3,4,5,6,7,8,9,0";
char[] sep ={ ',' };
string[] arr = allowedChars.Split(sep);
string fname = "";
string temp = "";
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
temp = arr[rand.Next(0, arr.Length)];
fname += temp;
}
return fname;
}

just call this like::
string getrandomname = GenerateRandomName;

Happy Coding..!!

Friday, September 26, 2008

code to store and retrieve image from sql server

Image tmpImg = pictureBox1.Image;
System.IO.MemoryStream memBuffer = new System.IO.MemoryStream();
tmpImg.Save(memBuffer, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] output = new byte[memBuffer.Length];
memBuffer.Read(output, 0, (int)memBuffer.Length);
// Write to SQL here

// Read from SQL here
memBuffer.Write(output, 0, output.Length);
tmpImg = Image.FromStream(memBuffer);
pictureBox1.Image = tmpImg;

Friday, September 12, 2008

How to ping a computer using windows application

// Pass host name or IP Address.
public void PingHost(string host)
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply = ping.Send(host);

MessageBox.Show("Status: " + pingReply.Status.ToString());
}
catch (System.Net.NetworkInformation.PingException e)
{
MessageBox.Show(e.Message);
}
}

private void button1_Click(object sender, EventArgs e)
{
PingHost(textBox1.Text.Trim());
}

Code for Lock Computer, VS 2005, c#

Follwing code is for Lock a Computer. It is nothing but calling of
“ C:\WINDOWS\system32\rundll32.exe user32.dll,LockWorkStation” command. There
are Command for shut down, log off , restart....


using System;
using System.Collections.Generic;
using System.Text;
//using System.linq;
using System.Diagnostics;

namespace r.LockComputer
{
class Program
{
static void Main(string[] args)
{
Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");
}
}
}

Tuesday, August 26, 2008

Store Image or Picture into DataBase and Retrieve from Database

Here is an example for How to store image into database and retrive that and display one by one

steps

*****

1. create one temp folder under root

2. save this 3 files into that

3. create database in ms-sql server

4. note : if connection not establishing with this driver , better to use jdbc :-> Odbc :-> driver

5. database code for ms-sqlserver

-create table image (uid numeric(6) identity (1001,1), img image )
-select * from image

getfile.html
************







Gettostore.jsp
**************

<%@ page language=”java” import=”java.sql.*,java.io.*” errorPage=”" %>
<%
try
{
Connection con = null;
System.setProperty( “jdbc.drivers”, “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
Class.forName( “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
con = DriverManager.getConnection( “jdbc:microsoft:sqlserver://SCALAR6:1433;DatabaseName=JAVATEAM;SelectMethod=cursor;User=sa;Password=ontrack20″ );
PreparedStatement pst = con.prepareStatement(”insert into image(img) values(?)”);

//logo path is a value which is come from file browser
FileInputStream fis=new FileInputStream(request.getParameter ( “path” ) );
byte[] b= new byte[fis.available()+1];
fis.read(b);
pst.setBytes(1,b);
pst.executeUpdate();
pst.close();
con.close();
}
catch(SQLException e)
{
out.println ( e);
}
catch (ClassNotFoundException e)
{
out.println( e );
}

%>

View from Database

link –>

Puttoview.jsp
*************

<%@ page contentType=”text/html; charset=iso-8859-1″ language=”java” import=”java.io.*,java.sql.*” errorPage=”" %>


Home

<%
try
{
Connection con = null;
System.setProperty( “jdbc.drivers”, “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
Class.forName( “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
con = DriverManager.getConnection( “jdbc:microsoft:sqlserver://SCALAR6:1433;DatabaseName=JAVATEAM;SelectMethod=cursor;User=sa;Password=ontrack20″ );
PreparedStatement pst = null;
ResultSet rs=null;
FileOutputStream fos;
int i = 0;
%>

<%
pst = con.prepareStatement(”select uid,img from image “); // better to use where uniqueid = ‘value’
rs=pst.executeQuery();
while(rs.next())
{
i = rs.getInt (1);
byte[] b=rs.getBytes(”img”);
fos=new FileOutputStream(”c://tomcat4.1/webapps/ROOT/temp/image” + i + “.jpg”);
fos.write(b);
fos.close();

%>

<%
}
pst.close();
con.close();
}
catch(SQLException e)
{
out.println ( e);
}
catch (ClassNotFoundException e)
{
out.println( e );
}

%>

.jpg” mce_src=”./image<%=i%>.jpg” >
<%=i%>


//run : http://localhost:8080/temp/getfile.html

Tricks with Server.MapPath

The Server.MapPath method can be used to retrieve several interesting properties about the running ASP.NET application, for example:

' the current directory
currDir = Server.MapPath(".")

' the parent directory
parentDir = Server.MapPath("..")

' the application's root directory
rootDir = Server.MapPath("/")

For example, you can store the application's root directory in an Application variable, and use it to dynamically build physical paths to other files and directories.

Happy Coding..!!