Asp. NET implementation of data Add (10th) _ Practical Skills

Source: Internet
Author: User
Tags trim

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.