Thursday, September 17, 2009

Q) What's Wrong in this? Getting "NullReferenceObject Exception"

C#:
I have getting "System.NullReferenceException: Object refernce not set to an instance of an object, at" .
This Exception is getting when giving the some text into the textbox's & Clicking Button of Windows Control Library form (usercontrol) which is placed the UC_submitcontrol usercontrol on the UCTestApp1 Windows Form and Execute.

See the Pictures:



Code for UC_SubmitControl1:
namespace UC_SubmitControl1
{
public partial class UC_SubmitScreen : UserControl
{
public delegate void SubmitClickHandler();
public event SubmitClickHandler SubmitbtnClicked;

public UC_SubmitScreen()
{
InitializeComponent();
}

private void btnSubmit_Click(object sender, EventArgs e)
{
if(tbText.Text.Length==0)
MessageBox.Show("Please Enter Some Text..!");
else
{
OnSubmitClicked();
}
}

protected virtual void OnSubmitClicked()
{
if (SubmitbtnClicked != null)
{
SubmitbtnClicked(); // Notify Subscribers
}
}

public string Prop_TextHere
{
get { return tbText.Text; }
set { tbText.Text = value; }
}
}
}


See the Picture for UC_TestApp1 [Windows Application]


Code for UC_TestApp1:
namespace UC_TestApp1
{
public partial class Form1 : Form
{
private UC_SubmitControl1.UC_SubmitScreen submitscrObj;
//private System.ComponentModel.Container components = null;


public Form1()
{
InitializeComponent();
}

private void uC_SubmitScreen1_SubmitbtnClicked()
{
try
{
MessageBox.Show(String.Format("Hello, {0}!", submitscrObj.Prop_TextHere));
//string aa = submitscrObj.Prop_TextHere;
//MessageBox.Show("Hello ", +aa.ToString()+" !");
//this.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
this.Close();
}
}
}
}

Monday, September 14, 2009

How do i make MDI Parent & Child Forms in C#.NET as looks like as Master & Child Pages in ASP.NET?

C#:
I have to develop project that looks like "Master & child" Pages in ASP.net, i.e., "MDI-Parent & MDI-child" But here i need not want the titlebar for child form and should not have child form minimizeble (i.e, ChildForm should be fit in the content part of a MDI-Parent form).

Here my idea is, First i have to design a MDI Parent form with companies Logo picture bar & 3 buttons(Rounded Rectangular image buttons) namely "EntryForm" button, "UpdateForm" button, "ViewForm" button. In that if i clicked "EntryForm" button, then the Entryform MDI-child should be should in that MDI-Parent Form and as the same for other two buttons also. Note: Even though the MDI-Child form should works as it as independent form

Note:

At Present i'm using the Custom User controls, But getting confusions in linking the custom user control forms. I have used (designed) 3 custom controls (UC_EntryForm, UC_UpdateForm, UC_ViewForm) as a child form looks. And One Normal Window Form with BackGround Companies Logo picture and a Panel (into which the custom controls will shown) & 3 buttons. For each button has needs a code to load a specific custom user control. How?



Thursday, September 10, 2009

How to write code : for every button click, should add the Date data (taking from textbox1) into "Sample" Table which as Date field


C#:In my application program, i have textbox named "Curdate" with my prefferred format. when i click a button1, the data (from the "Curdate" textbox) is add into the "CDate" field of the Table "DateTb".
if the table have not existed, then it should be create a table "DateTb" along with a field "CDate" and add the data (as row-by row)

Note: This the same process for i need to use the ExcelSheet, Ms-Access, SQLsever2005.

How to insert a Line of data in the middle of the Existiong Text File?

class InsertMidLine11
{
static string path = "@/../../test1.txt"; //refers to bin folder
static string data = "Line5";
static int lineno = 10;

static void Main(string[] args)
{
InsertIntoFile(path,data,lineno);
}

public static void InsertIntoFile(string Path, string Data, int LineNumber)
{
StringBuilder result = new StringBuilder();
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite);
int lines = 0;
using (StreamReader sr = new StreamReader(fs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (lines == (LineNumber - 1))
{
result.Append(Data);
result.Append(" | ");
}
result.Append(line);
result.Append(" | ");
lines++;
}
}
string fullstr = result.ToString();
string[] text = fullstr.Split('|');
using (StreamWriter sw = new StreamWriter(Path))
{
for (int i = 0; i < text.Length; i++)
{
string item = text[i].Trim();
sw.WriteLine(item);
Console.WriteLine(item);
}
}
}
}

That results:

Some Line1
Some Line2
Some Line3
Some Line4
Some Line5
Some Line6

Here i have to insert a Line "Some Line10" infront of the Line "Some Line5" that Looks as:

Some Line1
Some Line2
Some Line3
Some Line4
Some Line10
Some Line5
Some Line6


Note: first create test1.txt along above data and place this in the current project's bin folder.

Wednesday, September 9, 2009

Visual C# Samples, Specifications, Quick Reference and Visual C# General FAQ

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2d666562-ed08-4461-bf92-7808913b4e96

Number to Text in console Program



namespace Number2Text01
{
class Program
{
static string str;
static void Main(string[] args)
{
Console.Write("\n Enter Three Digit number::");
int amt = int.Parse(Console.ReadLine());
int temp,i;
//static string str;
string[] Hundreds = new string[] { "One Hundred", "Two hundred", "Three Hundreds", "Four Hundreds", "Five hundred", "Six hundred", "Seven Hundreds", "Eight hundred", "Nine hundred" };
string[] Tens = new string[] { "", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eight", "Ninty" };
string[] Ones = new string[] {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine" };

int[] arr = new int[3];
temp=amt;
i = 0;
while (temp != 0)
{
arr[i] = (temp % 10);
temp = temp / 10;
i++;
}
i -= 1;
while (i != -1)
{
if (i == 2)
str = Hundreds[arr[i]-1];
if (i == 1)
str += " and " + Tens[arr[i]];
if (i == 0)
str +=" " + Ones[arr[i]];
i--;
}
Console.WriteLine("\n\t Given number is "+amt+" and the Text is "+str);
Console.Read();
}
}
}

Number to Text1



namespace Number2Text1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string[] ones ={"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] tens ={ "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };
string[] hundreds = { "One hundred", "Two hundred", "Three hundred", "Four hundred", "Five hundred", "Six hundred", "Seven hundred", "Eight hundred", "Nine hundred" };
int Val, i;
string Res = " ";
Val = Convert.ToInt32(textBox1.Text);
if (Val > 999 && Val < 10000)
{
i = Val / 1000;
Res = ones[i-1] + " Thousand ";
Val = Val % 1000;
}
if (Val > 99 && Val < 1000)
{
i = Val / 100;
Res = Res + hundreds[i - 1];
Val = Val % 100;
}
if (Val > 19 && Val < 100)
{
i = Val / 10;
Res = Res + tens[i-1];
Val = Val % 10;
}
if (Val > 0 && Val < 20)
{
Res = Res + ones[Val-1];
textBox2.Text = Res;
}
textBox2.Text = Res;
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar > 47 && e.KeyChar < 58)||(e.KeyChar ==8)) != true)
e.Handled = true;
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
}
}
}

Followers