IT Community - Software Programming, Web Development and Technical Support

C# .Net Tips & Tricks

This is a discussion on C# .Net Tips & Tricks within the C# Programming forums, part of the Software Development category; hi... guys.... For my project, I am having table <sample table>. In that table have some null allowable ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 08-03-2007, 09:17 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default C# .Net Tips & Tricks

hi... guys....

For my project, I am having table <sample table>. In that table have some null allowable field also. I want to select all (or specific) the records from that table. I am working on .net frame work 2.0. In my project I am using table adapter for handling stored procedures and methods.

I got the null exception while I am fetching the records in that table through table adapter.
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...

Last edited by Booom : 08-17-2007 at 11:59 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 08-03-2007, 10:38 PM
smani smani is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 56
smani is on a distinguished road
Smile Re: Tips and Tricks for C# .Net

hi,
here is the solution for that,
1.Go to the property window of table adapter, and select particular column(in which null value u need to allow) make the AllowDBNull-true, and NullValue property-Empty or Null(by default it is (Throw exception))
but this tricks works only for varchar column

for other column we can go for option 2

2.In our stored procedure

Create or ALTER PROCEDURE [dbo].[spName]
(
@param uniqueidentifier
)
AS
SET NOCOUNT ON;
SELECT isNull(column2,'') as column3,isNull(column3,'') as column2 FROM TableName
where column1=@param

in both the case it will send empty string if null value comes
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 08-17-2007, 05:24 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Tips and Tricks for C# .Net

Hi,

This is the simple way to convert the jpeg image into gif with high quality.

Bitmap img = new Bitmap(200, 100, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(img);
Pen p = new Pen(Color.Black, 1);
Brush b = new SolidBrush(Color.Green);
System.IO.MemoryStream MS = new System.IO.MemoryStream();
g.Clear(Color.White);
g.FillRectangle(b, 20, 20, 40, 40);
g.DrawRectangle(p, 25, 25, 50, 50);
DateTime dt = DateTime.Now;
Font drawFont = new Font("Arial", 22);
SolidBrush drawBrush = new SolidBrush(Color.Red);
PointF drawPoint = new PointF(75F, 50F);
g.DrawString(dt.ToString("T", DateTimeFormatInfo.InvariantInfo), drawFont, drawBrush, drawPoint);
//Bitmap bitmappal = new Bitmap(MS);
img.Save(MS, ImageFormat.Gif);
byte[] buffer = new byte[MS.Length];
buffer = MS.ToArray();
Response.Clear();
Response.ContentType = "Image/Gif";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.End();

Thanks..

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 08-17-2007, 09:48 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Tips and Tricks for C# .Net

Hi,

This project will show a different way how to
- enter ONLY integer value in a TextBox
- enter ONLY the characters you want and avoid certain invalid characters in a TextBox

Let's start with TextBox 'textboxInteger'.

As mentioned before you can enter here ONLY integer values. Other characters will not be accepted.

Here is the method for input integer

private void textboxInteger_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar !=(char)8)
{
this.statusBar1.Text="incorrect key...";
e.Handled=true; // input is not passed on to the control(TextBox)
} else
this.statusBar1.Text="OK..";
}

TextBox 'textboxChars'

We want here to avoid entering the user certain invalid characters(i.e. 'a','b','c','#','*','1')
To solve this I implemented 3 methods in the Form1.cs
* mTextboxCharsKeyPressWithForeach
* mTextboxCharsKeyPressWithIF
* mTextboxCharsKeyPressWithSwitch that I prefer
private void mTextboxCharsKeyPressWithSwitch(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
this.statusBar1.Text="OK...";
switch(e.KeyChar)
{
case 'a':
case 'b':
case 'c':
case '#':
case '*':
case '1': //this is char not int
e.Handled=true; //it indicates the event is handled.
this.errorProvider1.SetError(this.textboxChars, "not allowed chars: 'a','b','c','#','*','1'");
this.statusBar1.Text="not allowed char..."+e.KeyChar;
break;
default:
this.errorProvider1.SetError(this.textboxChars, ""); //clear error
break;
} //switch
}
if the chars you typed are 'a','b','c','#','*','1' then we use an ErrorProvider and ToolTip control on the TextBox.
An error icon appears next to the TextBox. If you hold the mouse pointer over the error icon, a ToolTip appears displaying the error message.


Thanks...

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 08-18-2007, 01:41 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: Tips and Tricks for C# .Net

Hi,

VB.NET to C# conversion - tips

Keywords
in C# variables are ByVal by default, so no keyword is used for ByVal
use ref instead of VB's ByRef
C# also has the in and out keywords that you can use (you can look them up on MSDN)

use base in C# instead of VB's MyBase
use this in C# instead of VB's Me
to the best of my understanding VB's MyClass doesnt exist in C# so you should just use this (correct me if I'm wrong)
use null in C# instead of VB's Nothing

Some of the things that don't exist in C#
Optional parameters don't exist in C#. You can have multiple function definitions or if applicable, use params(scroll down and see the param array example)
The Redim keyword does not exist in C#
C# doesn't have Modules. Use a class instead
C# doesn't support With ... End With blocks

Examples:
vb: MustInherit class
c# abstract class

vb: NotInheritable class
c#: sealed class

vb: Public MustOverride Function foo() As Boolean
c#: public abstract bool foo();

vb: Public Shared counter As Integer
c#: public static int counter;

vb: Shared Function foo() As Boolean
c# static bool foo ()

vb: MustOverride Property name() As String
c#: abstract string name{get;set;}
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 08-18-2007, 03:20 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 200
krishnakumar is on a distinguished road
Smile Re: Tips and Tricks for C# .Net

Hi,
Vb to C# conversion..
Suppose u got some codings in vb language,if u want to change the language from VB to C# means, U can try this link, Here its,

CodeTranslator: Free Code Translation From VB.NET <-> C#
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily

Last edited by krishnakumar : 08-18-2007 at 03:24 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 08-20-2007, 09:36 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Tips and Tricks for C# .Net

Insert text at a specified line number in a file using C#

using System;
using System.IO;
using System.Collections;

namespace InsertLineInTextFile
{
class Program
{
static void Main (string[] args)
{
string strTextFileName = "myFile.txt";
int iInsertAtLineNumber = 2;
string strTextToInsert = "3. Apple";
ArrayList lines = newArrayList();
StreamReader rdr = newStreamReader(
strTextFileName);
string line;
while ((line = rdr.ReadLine()) != null)
lines.Add(line);
rdr.Close();
if (lines.Count > iInsertAtLineNumber)
lines.Insert(iInsertAtLineNumber,
strTextToInsert);
else
lines.Add(strTextToInsert);
StreamWriter wrtr = newStreamWriter(
strTextFileName);
foreach (string strNewLine in lines)
wrtr.WriteLine(strNewLine);
wrtr.Close();
}
}
}

Thanks..

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 08-21-2007, 01:46 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: C# .Net Tips & Tricks

Resize an array in C#

In C#, arrays cannot be resized dynamically. One approach is to use System.Collections.ArrayList instead of a native array. Another (faster) solution is to re-allocate the array with a different size and to copy the contents of the old array to the new array. The generic function resizeArray (below) can be used to do that.

public static System.Array ResizeArray (System.Array oldArray, int newSize)
{
int oldSize = oldArray.Length;
System.Type elementType = oldArray.GetType().GetElementType();
System.Array newArray = System.Array.CreateInstance(elementType,newSize);
int preserveLength = System.Math.Min(oldSize,newSize);
if (preserveLength > 0)
System.Array.Copy (oldArray,newArray,preserveLength);
return newArray;
}

public static void Main ()
{
int[] a = {1,2,3};
a = (int[])ResizeArray(a,5);
a[3] = 4;
a[4] = 5;
for (int i=0; i<a.Length; i++)
System.Console.WriteLine (a[i]);
}
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 08-22-2007, 09:09 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: C# .Net Tips & Tricks

Apply Water Mark to Image using C#:

1) Create a C# website.

2) Copy and paste the following code in aspx.cs page.

string Filename = "";
int Width, Height;
ImageFormat ImgFormat;
System.Drawing.Image Img;
Bitmap baseMap;

protected void Page_Load(object sender, EventArgs e)
{
Filename = @"D:\strelitzia.jpg";
CreateGraphic();
baseMap.Save(Response.OutputStream, ImageFormat.Jpeg);
baseMap.Dispose();
Img = null;
Response.End();
}
public void CreateGraphic()
{
SolidBrush letterBrush = new SolidBrush(Color.FromArgb(50, 255, 255, 255));
SolidBrush shadowBrush= new SolidBrush(Color.FromArgb(50, 0, 0, 0));
Font fontTitle= new Font("tahoma", 20, FontStyle.Bold);
//Filename = Request.QueryString["filename"];
//Filename = Server.MapPath(Filename);
ImgFormat = ImageFormat.Jpeg;
Response.ContentType ="image/jpeg";
Img = System.Drawing.Image.FromFile(Filename);
Width = Img.Width;
Height = Img.Height;
baseMap = new Bitmap(Width,Height);
Graphics myGraphic= Graphics.FromImage(baseMap);
myGraphic.DrawImage(Img, 0,0, Width,Height);
myGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
myGraphic.DrawString("WWW.DISCUSSWEB.COM", fontTitle, shadowBrush, 5, 60);
myGraphic.DrawString("WWW.DISCUSSWEB.COM", fontTitle, letterBrush, 7, 62);
Img.Dispose();
}

Thanks..

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 08-22-2007, 09:59 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: C# .Net Tips & Tricks

Apply DropShadow to image using C#:

Steps:

1) Create a C# website.

2) Copy and paste the following code

string Filename = "";
int Width, Height, shadowSize;
ImageFormat ImgFormat;
System.Drawing.Image Img;
Bitmap baseMap;
protected void Page_Load(object sender, EventArgs e)
{
Filename = @"D:\sample.jpg";
shadowSize = 10;
CreateGraphic();
baseMap.Save(Response.OutputStream, ImageFormat.Jpeg);
baseMap.Dispose();
Img = null;
Response.End();
}
public void CreateGraphic()
{
SolidBrush letterBrush = new SolidBrush(Color.FromArgb(50, 255, 255, 255));
SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(50, 0, 0, 0));
Font fontTitle = new Font("tahoma", 20, FontStyle.Bold);
//Filename = Request.QueryString["filename"];
//Filename = Server.MapPath(Filename);
ImgFormat = ImageFormat.Jpeg;
Response.ContentType = "image/jpeg";
Img = System.Drawing.Image.FromFile(Filename);
Width = Img.Width;
Height = Img.Height;
baseMap = new Bitmap(Width, Height);
Graphics myGraphic = Graphics.FromImage(baseMap);
myGraphic.FillRectangle(new SolidBrush(Color.White), 0, 0, Width + shadowSize, Height + shadowSize);
myGraphic.FillRectangle(new SolidBrush(Color.DarkGray), 10, 10, Width+shadowSize, Height+shadowSize);
myGraphic.FillRectangle(new SolidBrush(Color.Black), 0, 0, Width+6, Height+6);
myGraphic.DrawImage(Img, 2,2, Width+2,Height+2);
Img.Dispose();
}
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SAP Tips & Tricks leoraja8 Operating Systems 0 03-29-2008 12:11 AM
PHP Tips and Tricks Sabari PHP Programming 20 12-18-2007 05:26 AM
SQL Server Tips & Tricks Venkat Database Support 16 09-24-2007 01:34 AM
.NET tricks & Tips Karpagarajan VB.NET Programming 1 04-23-2007 08:17 AM
SEO Tips & Tricks spid4r Search Engine Optimization 0 03-08-2007 11:03 PM


All times are GMT -7. The time now is 01:46 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0