Saturday, September 27, 2008
GenerateRandomName for images in asp.net 2.0 using c#
{
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
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
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#
“ 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 );
}
%>
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;
%>
<%=i%> |
//run : http://localhost:8080/temp/getfile.html
Tricks with Server.MapPath
' 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..!!