C Sharp Program

13
C sharp program Uploading file using c sharp protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string filename = System.IO.Path.GetFileName(FileUpload1.FileName); string extension = System.IO.Path.GetExtension(filename); //Session["filepath"] = System.IO.Path.GetFullPath(filename); if (extension == ".pdf") { if (Label1.Text == "") { if (Label1.Text == "") { //FileUpload1.SaveAs("path of the folder\\" + filename); //Session["filepath"] = "path of the folder\\" + filename; //System.Security.Permissions.FileIOPermission = true; FileUpload1.SaveAs("path of the folder" + filename); Session["filepath"] = "path of the folder\\" + filename; } if (Page.IsValid) {  Addrecordtodatabase(); } } } else { Label1.Visible = true; Label1.Text = "Only Pdf file Allowed"; } }

Transcript of C Sharp Program

Page 1: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 1/13

C sharp program

Uploading file using c sharp

protected void Button1_Click(object sender, EventArgs e)

{

if (FileUpload1.HasFile)

{

string filename = System.IO.Path.GetFileName(FileUpload1.FileName);

string extension = System.IO.Path.GetExtension(filename);

//Session["filepath"] = System.IO.Path.GetFullPath(filename);

if (extension == ".pdf")

{

if (Label1.Text == "")

{

if (Label1.Text == "")

{

//FileUpload1.SaveAs("path of the folder\\" + filename);

//Session["filepath"] = "path of the folder\\" + filename;

//System.Security.Permissions.FileIOPermission = true;

FileUpload1.SaveAs("path of the folder" + filename);

Session["filepath"] = "path of the folder\\" + filename;

}

if (Page.IsValid)

{

 Addrecordtodatabase();

}

}

}

else

{

Label1.Visible = true;

Label1.Text = "Only Pdf file Allowed";

}

}

Page 2: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 2/13

else

{

Label1.Visible = true;

Label1.Text = "You have not uploaded the file";

}

}

protected void Addrecordtodatabase()

{

try

{

String sql = "";

sql = "Insert into pdftbl(title,filepath)values('" + titletxt.Text + "','"+Session["filepath"].ToString()+"')";

SqlConnection con = new SqlConnection();

con.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];

con.Open();

SqlCommand com = new SqlCommand(sql, con);

com.ExecuteNonQuery();

con.Close();

clearcontent(this);

Page.RegisterStartupScript("myscript", "<script languange=javascript>alert('File has been uploaded

Successfully')</script>");

}

catch (Exception e)

{

throw (e);

}

}

protected void clearcontent(System.Web.UI.Page cont)

{foreach (Control ctrl in cont.Form.Controls)

{

if (ctrl.GetType() == typeof(TextBox))

{

((TextBox)ctrl).Text = "";

}

}

}

Page 3: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 3/13

 

Coloring of mdi form

The hierarchy for the File menu should look like this.

File

->New

-->Original

-->Blue

-->Red

->Exit

It's better to understand with a screenshot:

 As you know from Microsoft Word and all the other MDI applications, the creation of new child forms is made

from a menu or a toolbar. In this tutorial we use a menu.

We choosed in this tutorial to be able to build 3 types of child forms, 'Original', 'Blue' and 'Red'.

Page 4: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 4/13

Let's see how we can create a child form at runtime, as this is the main scope of the tutorial.

Double click the 'Original' item from the File -> New menu.

Visual C# .NET sends us to the part where we code:

private void mnuFileNewStandard_Click(object sender, System.EventArgs e)

{

}

Replace the code with the following: 

private void mnuFileNewStandard_Click(object sender, System.EventArgs e)

{

// Create a new form

Form standardFrm = new Form();

// Give it a title

standardFrm.Text = "Standard Form";

// Who's child is this window

standardFrm.MdiParent = this;

// Show it

standardFrm.Show();

}

In the first line we create a new form named 'standardFrm'. The new form is created like any other form. In the

second we give it a generic title.

In the third we specify the parent of the newly created window by setting MdiParent to the current main window

(this). This makes the form appear in the MDI form.

Finally the form is displayed.

It's time to make the blueFrm. Double click the 'Blue' item from the File -> New menu.

 After Visual C# .NET takes you to the part where you code, use the following code: 

private void mnuFileNewBlue_Click(object sender, System.EventArgs e)

{

// Create a new form

Form blueFrm = new Form();

Page 5: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 5/13

 

// Give it a title

blueFrm.Text = "Blue Form";

// Change the background color of the form

blueFrm.BackColor = System.Drawing.Color.Blue;

// Make the child form maximized

blueFrm.WindowState= System.Windows.Forms.FormWindowState.Maximized;

// Who's child is this window

blueFrm.MdiParent = this;

// Show it

blueFrm.Show();

}

 Again we create a new form, this time named 'blueFrm' and we give it a title.

We set the background color of the new form to blue in the third line of code.

Next we set the WindowsState of the Window to Maximized.

We assign the window to the parent window (MDI) and display it.

 After you compile the code create a new Standard form and a Blue form.

 As you saw, the Blue form is maximized. Click the 'Windows' menu and you will see the list of available openedforms and a checkmark next to the one selected. Using this menu you can easily switch between forms.

Finally, let's make the code for redFrm by double clicking the 'Red' item from the File -> New menu and inserting

this code: 

private void mnuFileNewRed_Click(object sender, System.EventArgs e)

{

Page 6: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 6/13

// Create a new form

Form redFrm = new Form();

// Give it a title

redFrm.Text = "Red Form";

// Change the background color of the form

redFrm.BackColor = System.Drawing.Color.Red;

// Create a new button

Button newStandardBtn = new Button();

// Set the width of the button

newStandardBtn.Width = 120;

// Set the caption of the button

newStandardBtn.Text = "New Standard form";

// Set the position of the button

newStandardBtn.Left = (redFrm.Width - newStandardBtn.Width) / 2;

newStandardBtn.Top = 20;

// When the button is clicked create a Standard form

newStandardBtn.Click += new System.EventHandler(this.mnuFileNewStandard

_Click);

// Add the button to the form

redFrm.Controls.Add(newStandardBtn);

// Who's child is this window

redFrm.MdiParent = this;

// Show it

redFrm.Show();

}

 As you can see, the first lines do the same thing as the earlier examples.

On the next lines we create a button named 'newStandardBtn' and give it a width of 120 pixels. Next we set this

button's Text property to 'New Standard form' - because it will create a new Standard form.

We horizontally center the button by finding the center of the form (substract the width of the button from the form

and divide the result by 2) and set the vertical position 20 pixels far from the top.

When the Click event of the button occurs, that is when the user clicks the button we want to act the same as if 

he was clicking the Standard item from the File -> New menu.

Page 7: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 7/13

 After that the button is added to the form and we're finished!

The following is a screenshot of our brand new ugly red form:

If you want to add functionality to the 'Exit' item in the 'File' menu add the following code after double clicking on

it: 

private void mnuFileExit_Click(object sender, System.EventArgs e)

{

Application.Exit();

} The hierarchy for the File menu should look like this.

File

->New

-->Original-->Blue

-->Red

->Exit

It's better to understand with a screenshot:

 As you know from Microsoft Word and all the other MDI applications, the creation of new child forms is made

Page 8: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 8/13

from a menu or a toolbar. In this tutorial we use a menu.

We choosed in this tutorial to be able to build 3 types of child forms, 'Original', 'Blue' and 'Red'.

Let's see how we can create a child form at runtime, as this is the main scope of the tutorial.

Double click the 'Original' item from the File -> New menu.Visual C# .NET sends us to the part where we code:

private void mnuFileNewStandard_Click(object sender, System.EventArgs e)

{

}

Replace the code with the following: 

private void mnuFileNewStandard_Click(object sender, System.EventArgs e)

{

// Create a new form

Form standardFrm = new Form();

// Give it a title

standardFrm.Text = "Standard Form";

// Who's child is this window

standardFrm.MdiParent = this;

// Show it

standardFrm.Show();

}

In the first line we create a new form named 'standardFrm'. The new form is created like any other form. In the

second we give it a generic title.

In the third we specify the parent of the newly created window by setting MdiParent to the current main window(this). This makes the form appear in the MDI form.

Finally the form is displayed.

It's time to make the blueFrm. Double click the 'Blue' item from the File -> New menu.

 After Visual C# .NET takes you to the part where you code, use the following code: 

private void mnuFileNewBlue_Click(object sender, System.EventArgs e)

Page 9: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 9/13

{

// Create a new form

Form blueFrm = new Form();

// Give it a title

blueFrm.Text = "Blue Form";

// Change the background color of the form

blueFrm.BackColor = System.Drawing.Color.Blue;

// Make the child form maximized

blueFrm.WindowState= System.Windows.Forms.FormWindowState.Maximized;

// Who's child is this window

blueFrm.MdiParent = this;

// Show itblueFrm.Show();

}

 Again we create a new form, this time named 'blueFrm' and we give it a title.

We set the background color of the new form to blue in the third line of code.

Next we set the WindowsState of the Window to Maximized.

We assign the window to the parent window (MDI) and display it.

 After you compile the code create a new Standard form and a Blue form.

 As you saw, the Blue form is maximized. Click the 'Windows' menu and you will see the list of available opened

forms and a checkmark next to the one selected. Using this menu you can easily switch between forms.

Finally, let's make the code for redFrm by double clicking the 'Red' item from the File -> New menu and inserting

this code: 

Page 10: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 10/13

 

private void mnuFileNewRed_Click(object sender, System.EventArgs e)

{

// Create a new form

Form redFrm = new Form();

// Give it a title

redFrm.Text = "Red Form";

// Change the background color of the form

redFrm.BackColor = System.Drawing.Color.Red;

// Create a new button

Button newStandardBtn = new Button();

// Set the width of the button

newStandardBtn.Width = 120;

// Set the caption of the button

newStandardBtn.Text = "New Standard form";

// Set the position of the button

newStandardBtn.Left = (redFrm.Width - newStandardBtn.Width) / 2;

newStandardBtn.Top = 20;

// When the button is clicked create a Standard form

newStandardBtn.Click += new System.EventHandler(this.mnuFileNewStandard

_Click);

// Add the button to the form

redFrm.Controls.Add(newStandardBtn);

// Who's child is this window

redFrm.MdiParent = this;

// Show it

redFrm.Show();

}

 As you can see, the first lines do the same thing as the earlier examples.

On the next lines we create a button named 'newStandardBtn' and give it a width of 120 pixels. Next we set this

button's Text property to 'New Standard form' - because it will create a new Standard form.

We horizontally center the button by finding the center of the form (substract the width of the button from the form

and divide the result by 2) and set the vertical position 20 pixels far from the top.

Page 11: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 11/13

 

When the Click event of the button occurs, that is when the user clicks the button we want to act the same as if 

he was clicking the Standard item from the File -> New menu.

 After that the button is added to the form and we're finished!

The following is a screenshot of our brand new ugly red form:

If you want to add functionality to the 'Exit' item in the 'File' menu add the following code after double clicking on

it: private void mnuFileExit_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

Fill the datagrid view with tha database data

In this tutorial, I'm going to show a fairly simple way to bind a .NET DataGridView to a database. The form

designer has some support for data binding, but I found doing it without the form designer is a little easier to

understand and implement. Also, when I'm developing a desktop database application, my database schemas

are rarely 100% defined, and the form designer doesn't easily support changes to the database.

The database I'm going to use for the example code will be an Access database. I know Access databases aren't

the preferred database type for developers - because of their speed and scalability. However, for simple

database apps, Access is hard to beat - since you don't need to install any outside database engines. In reality,

the concepts shown in this tutorial can be used with any number of databases.

Page 12: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 12/13

The first thing we'll need to do is generate a connection string to connect to our Access database. For simplicity,

the database I'm using doesn't require any authentication. If your database has authentication, MSDN has

some great documentation on how to accomplish that.string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\myDatabase.mdb";

The connection string is broken up into two parts, a provider and a data source. The provider is the engine we're

going to be using - in this case, Microsoft's Jet engine. The data source, for Access, is simply the path to thedatabase file.

Now let's use the connection string and get some data from our database.

//create the connection string  string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\myDatabase.mdb";

//create the database query  string query = "SELECT * FROM MyTable";

//create an OleDbDataAdapter to execute the query  OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

//create a command builder  

OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

//create a DataTable to hold the query results DataTable dTable = new DataTable();

//fill the DataTable dAdapter.Fill(dTable);

To keep the code simple, I've left out a lot of error handling. You'll definitely want to

surround dAdapter.Fill with some exception handling. That call will fail for many different reasons - for 

instance the database isn't where the connection string says it is, or the query string is invalid SQL code.

So let's go through what this code is actually doing. The first thing to do is to create the connection string as

described above. Then we need an SQL statement to execute on our database. This can be any SELECT

statement you want. Next we create anOleDbDataAdapter which serves as a bridge between our DataTable and our database. An

OleDbCommandBuilder comes next. This beautiful object automatically generates SQL insert, update, and

delete statements to rectify changes made to our DataTable. Next we need to make a DataTable to hold the

information retrieved from the database. And lastly, we call dAdapter.Fill(dTable) which executes our SQL

query and fillsdTable with the results.

Now that we have a DataTable filled with database information, let's see how to synchronize it with

aDataGridView.//the DataGridView  DataGridView dgView = new DataGridView();

//BindingSource to sync DataTable and DataGridView  BindingSource bSource = new BindingSource();

//set the BindingSource DataSource bSource.DataSource = dTable;

//set the DataGridView DataSource dgView.DataSource = bSource;

The BindingSource object is what will be keeping our DataTable synchronized with the DataGridView. So

we set the DataSource of the BindingSource to dTable, then set the DataSource of the DataGridView to

theBindingSource. Now when your program runs, the DataGridView should be filled with the results of your 

SQL query.

 At point, any changes made by the user in the DataGridView will automically be made to the DataTable, dTable.Now we need a way to get the changes back into the database. All you have to do is call the Update function of 

theOleDbDataAdapter with the DataTable as the argument to accomplish this.

Page 13: C Sharp Program

8/3/2019 C Sharp Program

http://slidepdf.com/reader/full/c-sharp-program 13/13

dAdapter.Update(dTable);

This call will use the OleDbCommandBuilder to create all of the necessary SQL code to synchronize your 

database with the changes made to dTable. But, when should you call this function? There's lots of different

answers to that. If you have a save button, call it when the user pushes save. If you want the database updated

in real-time, I like to call it on the DataGridView's Validating event.

1.  using System;

2.  using System.Collections.Generic;3.  using System.Linq;

4.  using System.Text;

5.  namespace ConsoleApplication1

6.  { 

7.  class Myclass

8.  { 

9.  static void  Main(string[] args) 

10. { 

11. Myclass d = new Myclass();

12. d.Myprog("ahasda");

13. } 

14. void  Myprog( params string[] s) 15. { 

16. int num=0;

17. foreach (string r in s) 

18. { 

19. if (r.Contains('a')) 

20. { 

21. num++;

22. } 

23. } 

24. Console.WriteLine(num);

25. } 

26. } 27. }