Archive for the ‘Programming’ Category

Convert Text to Image using c#

We can Convert text to image in c# using System.Drawing namespace.
Here we create a bitmap image and use its Graphics property to convert text passed to image and place the image in a picture box.

private void TextToImage()
{
Color BackColor = Color.White;
String FontName = "Times New Roman";
int FontSize = 25;
int Height = 50;
int Width = 300;
<span style="color: rgb(56, 118, 29);">
//creating bitmap image
<span style="color: rgb(0, 0, 0);">Bitmap </span></span>bitmap = new Bitmap(Width, Height);
 
<span style="color: rgb(56, 118, 29);">//FromImage method creates a new Graphics from the specified Image.</span>
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray;
Font font = new Font(FontName, FontSize); 
 
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
 
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
<span style="color: rgb(56, 118, 29);">//Drawing a rectangle and filling the background as white
<span style="color: rgb(0, 0, 0);">graphics</span></span>.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle); <span style="color: rgb(56, 118, 29);">
 
//giving the font and color to the string passed</span>
graphics.DrawString(txtEnter.Text,font,Brushes.Red, 0, 0);
pictureBox1.Image = bitmap;
 
}

Download

Popularity: 13% [?]

JavaBeans

Requirements for JavaBeans:

  • there have to be set/get methods specified
  • has to have public default constructor (without parameters)
  • it should be in a package
  • serializable
  • get/set methods has to have the same data type!
  • bean cannot modify content of a page!

… in general, JavaBean is a standard Class.

It can be used as a simple component in a JSP page.

- get method has to be fast, and it should be plain and simple.

usage in a JSP page:

<jsp:useBean id=”nameOfBean” scope=”session” class=”myPackage.Counter”>

<jsp:setProperty name=”nameOfBean” property=”myCount”>

this is the same as

<%= Counter.myCount %>

with JSTL Expression language:

<c:out value=”${Counter.myCount}”>

automatically set the bean attributes from names (!important!):
<jsp:setProperty name=”pocetMiest” property=”*”>

Scope can be:

  • application - the bean is available in all application, when that’s in more JSP pages, one of them can create it once and other JSP pages can use it. It’s something like a global object for all application.
  • session - stored data, just for this session
  • request - just for request or forward
  • page - smallest scope, just on a page that is called (forwarded or included)

Popularity: 1% [?]

Get available server names of Sql Server within the local network – SqlDataSourceEnumerator

SqlDataSourceEnumerator is a class in .net framework that gets the available instances of SQL Server within the local network.

DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();

SqlDataSourceEnumerator.Instance.GetDataSources() returns a datatable with the available servers in the current network.

The DataTable returned as 4 colunms such as :-

  • ServerName
  • InstanceName
  • IsClustered
  • Version

Popularity: 2% [?]

Sorting Program using C#

namespace SortingNos
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Enter Numbers :");
           int[] arr = new int[5];
           string arr1 = "";
           for (int i = 0; i < 5; i++)
 {
 arr1 = Console.ReadLine();
 arr[i] = Convert.ToInt32(arr1);            
}
 
 int a, b, temp = 0;
 Console.WriteLine();
 for (a = 0; a < arr.Length; a++)
           {
                               for(b=a+1;b< arr.Length;b++)
               {
                   if (arr[a] > arr[b])
                   {
                       temp = arr[a];
                       arr[a] = arr[b];
                       arr[b] = temp;
                   }
               }             
               Console.WriteLine(arr[a]);              
           }          
          Console.ReadLine();
       }
   }
}

Download

Popularity: 4% [?]

Create backup copy of table in SQLServer – (Select Into)

SELECT INTO statement can be used to create backup copy of a table.This sql statement selects records from one table according to the selection we make and inserts the result set into another new table with the table name we specify.

Syntax given below is used to make a exact copy of the table.That is it creates the table with the same no of columns and all records in the old table.

Syntax : Exact copy of old table

SELECT *
INTO new_table_name
FROM old_table_name

Example :

select * into employee_backup from employee

In this example a exact copy of the table employee is backed up as a new table employee_backup.

Syntax given below is used to make a new table with selected columns from old table.

Syntax : Selected columns from old table

SELECT column_name(s)
INTO new_table_name
FROM old_table_name

Example :

select employee_id,employee_name into employee_backup from employee

In this example a copy of the table employee with only columns “employee_id,employee_name” is backed up as a new table employee_backup.

Popularity: 9% [?]

Open a File at any format in JSP

How to Open a File in Different Format

1.Create a HTML Page or JSP Page
2.Insert <%@ page contentType=”application/vnd.ms-excel” %> instead of <%@ page contentType=”application/html” %> in HTML Page or JSP Page
3.Open a File,Now its open in a Excel Sheet with Style apply in the Sheet.

For Microsoft Word

<%@ page contentType=”application/vnd.msword” %>

For PDF File

<%@ page contentType=”application/vnd.pdf” %>

For Wordpad

<%@ page contentType=”application/vnd.rtf” %>

For Microsoft Powerpoint

<%@ page contentType=”application/vnd.ms-powerpoint” %>

For Winzip

<%@ page contentType=”application/vnd.zip” %>

Popularity: 3% [?]

Usage of Stored Procedures

Reduce network traffic: You have to send the SQL statement across the network. With sprocs, you can execute SQL in batches, which is also more efficient.

Caching query plan: The first time the stored procedure is executed, SQL Server creates an execution plan, which is cached for reuse. This is particularly performant for small queries run frequently.

Ability to use output parameters: If you send inline SQL that returns one row, you can only get back a recordset. With stored procedures you can get them back as output parameters, which is considerably faster.

Permissions: When you send inline SQL, you have to grant permissions on the table(s) to the user, which is granting much more access than merely granting permission to execute a stored procedure

Separation of logic: Remove the SQL-generating code and segregate it in the database.

Ability to edit without recompiling: This can be controversial. You can edit the SQL in a sproc without having to recompile the application.

Find where a table is used: With stored procedures, if you want to find all SQL statements referencing a particular table, you can export the stored procedure code and search it. This is much easier than trying to find it in code.

Optimization: It’s easier for a DBA to optimize the SQL and tune the database when stored procedures are used. It’s easier to find missing indexes and such.

SQL injection attacks: Properly written inline SQL can defend against attacks, but stored procedures are better for this protection.

Popularity: 2% [?]

How to Upload File in Asp.net

Here in this example we browse a file from our pc and upload that file to some other location in our pc.

Default.aspx

<input type=file id=File1 name=File1 runat=”server”>
<input type=”submit” id=”Submit1″ value=”Upload” runat=”server” name=”Submit1″>

Defalut.aspx.cs

protected void Submit1_ServerClick(object sender, EventArgs e)
{
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

//Create Data Folder within the Location u Specified
string SaveLocation = “D:\UploadedFiles” + “\” + fn;

//File will be Uploaded to the Specified Location
try
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write(“The file has been uploaded.”);
}
catch ( Exception ex )
{
Response.Write(“Error: ” + ex.Message);
}
}
else
{
Response.Write(“Please select a file to upload.”);
}
}

You can give the specified location to upload in SaveLocation string.

Download

Popularity: 1% [?]

Simple Add,Edit,Retrieve and Delete in Asp.net

Here is a example application of Add,Edit,Retrieve and Delete operations using Asp.net. Download Creating a table employeeinfo for proceeding the operations. Sql Table Creation

CREATE TABLE employeeinfo ( empid int NOT NULL PRIMARY KEY, empname varchar(50), empaddress varchar(200), empsalary int )

Declaring Connection String and Variables

Namespace : using System.Data.SqlClient;  string QryStr = ""; SqlConnection con = new SqlConnection("Server=servername;Integrated Security=true;Database=employee"); SqlCommand cmd; SqlDataReader dr;

In this example i have used windows authentication.You have to change the connection string according your specification. Adding Records

QryStr = "INSERT INTO employeeinfo VALUES('" +txtEmpId.Text + "','" + txtEmpName.Text + "','" + txtEmpAdd.Text + "','" + txtEmpSal.Text + "')"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Updating Records

QryStr = "UPDATE employeeinfo SET empname = '"+txtEmpName.Text+"',empaddress = '"+txtEmpAdd.Text+"',empsalary = '"+txtEmpSal.Text+"' WHERE empid = '"+txtEmpId.Text+"'"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Retrieve Records to textboxes Records will be Retrieved from the database and displayed in the text boxes when text change of employee id is fired.

QryStr = "SELECT * FROM employeeinfo WHERE empid = '" + txtEmpId.Text + "'";  con.Open(); cmd = new SqlCommand(QryStr, con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows) { txtEmpName.Text = dr["empname"].ToString(); txtEmpAdd.Text = dr["empaddress"].ToString(); txtEmpSal.Text = dr["empsalary"].ToString(); } else { txtEmpName.Focus(); } con.Close();

Deleting Records

QryStr = "DELETE FROM employeeinfo WHERE empid = '" + txtEmpId.Text + "'"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Popularity: 2% [?]

Absolute Position for Controls in Asp.net

By default for controls placed in the designer the layout position is as ‘Not Set’ thus we cannot drag the control to a specified location.


We have to change the layout position to absolute position to achieve that.

In Visual Studio we can make the default postion as we need.For that we have to enable the css postioning of controls.

Here is how we can do

1. Go to Layout->Position->Auto-position Options->HTML Designer–>CSS Styling

or

2. Go to Tools->Options->HTML Designer–>CSS Positioning

And check “Change positioning to the following for controls added using Toolbox, paste or drag and drop” and select the postion you perfer.Here we select Absolute Positioning.

Now we can move the controls placed in the designer with absolute positioning.

The Given Procedure is for Visual Studio 2005 which is same for Visual Studio 2008.

Popularity: 12% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars