Sunday, May 31, 2009

How To: Enable Allow Paging in Repeater Using C#

1- I'll Show You how to make a repeater that allow paging.
2- I'll Make a web user control and anyone Can use it in his project.

Steps:

1- Create A web user control using C# and rename it to RepeaterPager.

2- add four Buttons and one label:
Control Name Text

Button btnNext >
Button btnPrevious <
Button btnLastRecord >>
Button btnFirstRecord <<

3- Create property for Object Data Source:
public ObjectDataSource Ods { get; set; }

4- Create Property for Repeater Control Or DataList As you like:
public Repeater Rep { get; set; }

5- Create Property Get Current Page:

public int CurrentPage
{
get
{
object obj = this.ViewState["_CurrentPage"];
if (obj == null)
{
return 0;
}
else
{
return (int)obj;
}
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}


6- Bind Data in Repeater Control and Return number of Pages:


public int ItemsGet()
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = Ods.Select();
int x = objPds.Count;
objPds.AllowPaging = true;
objPds.PageSize = 1;
objPds.CurrentPageIndex = CurrentPage;
if (objPds.Count > 0)
{
btnPrevious.Visible = true;
btnNext.Visible = true;
btnLastRecord.Visible = true;
btnFirstRecord.Visible = true;
lblCurrentPage.Visible = true;
lblCurrentPage.Text = "Page: " + Convert.ToString(CurrentPage + 1) + " of " + Convert.ToString(objPds.PageCount);
}
else
{
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLastRecord.Visible = false;
btnFirstRecord.Visible = false;
lblCurrentPage.Visible = false;
}
btnPrevious.Enabled = !objPds.IsFirstPage;
btnNext.Enabled = !objPds.IsLastPage;
btnLastRecord.Enabled = !objPds.IsLastPage;
btnFirstRecord.Enabled = !objPds.IsFirstPage;
Rep.DataSource = objPds;
Rep.DataBind();
return x;
}


7- Buttons Controls Code:

protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
ItemsGet();
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
ItemsGet();
}
protected void btnLastRecord_Click(object sender, EventArgs e)
{
CurrentPage = ItemsGet() -1;
ItemsGet();
}
protected void btnFirstRecord_Click(object sender, EventArgs e)
{
CurrentPage = 0;
ItemsGet();
}

8- in (Web User Control) Load Event call GetItems Property:

protected void Page_Load(object sender, EventArgs e)
{
ItemsGet();
}


How To Use this Control:
1- Create ASP.Net Page
2- Add Repeater1 and ObjectDataSource1 to your page
3- in Page_load Event Put the following code:
protected void Page_Load(object sender, EventArgs e)
{
RepeaterPager.Ods= Repeater1;
RepeaterPager.Rep= ObjectDataSource1;
}
4- Build and run :)

Saturday, May 23, 2009

How To: Use Office 2007 OCR Using C#

I'll Show you how to read text from any image.

If you have Office 2007 installed, the OCR component is available for you to use. The only dependency that's added to your software is Office 2007. Requiring Office 2007 to be installed in order for your software to work may or may not fit a situation. But if your client can guarantee that machines that your software will run on have Office 2007 installed, you're gold. I've encountered many situations where this is the case. I've even encountered a few situations where clients were willing to install Office 2007 in order to use my applications.

Steps:

1- add Reference to Office 2007 Component:

The name of the COM object that you need to add as a reference is Microsoft Office Document Imaging 12.0 Type Library. By default, Office 2007 doesn't install it. You'll need to make sure that it's added by using the Office 2007 installation program. Just run the installer, click on the Continue button with the "Add or Remove Features" selection made, and insure that the imaging component is installed as shown in the figure to the right.
Important Note:The name of the COM object that you need to add as a reference is Microsoft Office Document Imaging 12.0 Type Library. By default, Office 2007 doesn't install it. You'll need to make sure that it's added by using the Office 2007 installation program. Just run the installer, click on the Continue button with the "Add or Remove Features" selection made, and insure that the imaging component is installed.

2- Create Windows Application Using C#:

from Visual Studio Solution Explorer >> right click on refferences>> select com tab>> then select (Microsoft Office Document Imaging 12.0 Type Library)

3- Put Button in your Form then put the following code:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();

MODI.Document md = new MODI.Document();

md.Create(openFileDialog.FileName);
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

MODI.Image image = (MODI.Image)md.Images[0];

MessageBox.Show(image.Layout.Text, "The Selected Image Text is:");

4- Run The Application then press the button and select any image has text.

Conclusion:

I made a big sample application for Office OCR, if anyone interested, you can contact me on :

waleed.hussein.eg@gmail.com

Friday, May 22, 2009

How To: Sending Email Using C#

in this article I'll show you how can you send mail using windows application
I'm using gmail becouse it allowed sending and recieving Emails and you need to make some configurations like:
Host Name: smtp.gmail.com
Port Number: 587
Your Email Address: yourEmail@gamil.com
your Password: your Email Password
SSL:true (becouse gmail using ssl)

Let's start...

Create Windows Application using C# :
Create Form Like the Below Form with Controls

Important Note Controls Names Will be Like:

textbox =txt...
label=lbl...
button=btn...




using System.Net;
using System.Net.Mail;


then create some properities :
you can put them in seperated class but I'm putted them in the same form because it's a sample

public string MailFrom { get; set; }
public string MailTo { get; set; }
public string MailBody { get; set; }
public string MailSubject { get; set; }
public string AttachmentFile { get; set; }
public string HostName { get; set; }
public int PortNumber { get; set; }
public string EmailAddress { get; set; }
public string EmailPassword { get; set; }
public bool SSL { get; set; }


then create method for sending mail:

public void SendingMail()
{
MailMessage mailMessage = new MailMessage(MailFrom,MailTo,MailSubject,MailBody);
Attachment fileAttachment = new Attachment(AttachmentFile);
mailMessage.Attachments.Add(fileAttachment);
SmtpClient smtpClient = new SmtpClient(HostName,PortNumber);
smtpClient.Credentials = new NetworkCredential(EmailAddress,EmailPassword);
smtpClient.EnableSsl = SSL;
smtpClient.Send(mailMessage);
}


in attachment button put the following code:

private void btnAttachment_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
lblAttachment.Text = fileDialog.FileName;
}


in Send Mail Button put the following code:

private void btnSendMail_Click(object sender, EventArgs e)
{
try
{
MailFrom = txtMailAddress.Text; ;
MailTo = txtTo.Text;
MailBody = txtBody.Text;
MailSubject = txtSubject.Text;
AttachmentFile = lblAttachment.Text;
HostName = txtHostName.Text;
PortNumber = Convert.ToInt16(txtPortNo.Text);
EmailAddress = txtMailAddress.Text;
EmailPassword = txtMailPassword.Text;
SSL = chkSSL.Checked;
SendingMail();
MessageBox.Show("Sending Mail Succeeded");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}