This section takes news websites as an example to add news and upload attachments and pictures to the server.
Learning content
step by adding a new item, creating a Web Form and naming it "newsadd.aspx"
Step two layout page, create a table with 6 rows 2 columns
Step three data source controls define the data by writing the code in the Newschuli.cs page as follows:
public static void Addnews (Newsclass news1)
{
using (SqlConnection cn = dbconn. Getconnection ())
{
CN. Open ();
SqlCommand cm = new SqlCommand ();
Cm. Connection = cn;
String sql = "Insert News (Title,contents,img,annex,time) VALUES (@aa, @bb, @cc, @dd, @ee)";
Cm.commandtext = SQL;
Cm. Parameters.addwithvalue ("@aa", news1. Title);
Cm. Parameters.addwithvalue ("@bb", news1. Contents);
Cm. Parameters.addwithvalue ("@cc", news1. IMG);
Cm. Parameters.addwithvalue ("@dd", news1. Annex);
Cm. Parameters.addwithvalue ("@ee", news1. time);
Cm. ExecuteNonQuery ();
}
Step four Open the Newsadd.aspx design page and double-click the Button1 button to write code:
protected void Button1_Click (object sender, EventArgs e)
{
newsclass = null;
NEWS1 = new Newsclass ();
News1. Title = TextBox1.Text.ToString (). Trim ();
News1. Contents= TextBox2.Text.ToString (). Trim ();
News1. IMG = FileUpload1.FileName.ToString ();
News1. annex= FileUpload2.FileName.ToString ();
News1. Time = DateTime.Now.ToString ();
Newschuli.addnews (NEWS1);
Response.Redirect ("newscategoriesmanager.aspx");
}
Step five will upload the pictures or attachments to the site, under the button buttons, write code:
if (fileupload1.hasfile)
{
fileupload1.saveas (Server.MapPath) ("..."). /upfile "+" \ "+ DateTime.Now.ToString (" Yyyymmddhhmmss ") + Fileupload1.filename));
if (fileupload2.hasfile)
{
fileupload2.saveas (Server.MapPath) ("..."). /upfile "+" \ "+datetime.now.tostring" ("YYYYMMDDHHMMSS") + Fileupload2.filename));
Step six Run
Run Effect chart:
Summarize
Through this section of the study, the implementation of the data added to improve the news of the basic operations: Find, update, and learn today to add tasks. On this basis also expanded a knowledge point-how to upload pictures or attachments to the site, I hope today's study, we and small series have been harvested.