cs 文件:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace shibing.admin
{
/// <summary>
/// WriteNews 的摘要说明。
/// </summary>
public class WriteNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btn_Replay;
protected System.Web.UI.WebControls.DropDownList DD_kind;
protected System.Web.UI.WebControls.Button btn_Reset;
protected System.Web.UI.WebControls.TextBox tb_Date;
protected System.Web.UI.WebControls.TextBox tb_Title;
protected System.Web.UI.WebControls.TextBox tb_Source;
protected System.Web.UI.WebControls.TextBox tb_Content;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
     myweb.News mywebNews=new myweb.News();

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{ }// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.btn_Replay.Click += new System.EventHandler(this.btnReplay_Click);
this.btn_Reset.Click += new System.EventHandler(this.btn_Reset_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnReplay_Click(object sender, System.EventArgs e)
{
ArrayList CommAry=new ArrayList();//定义并初始一个rrayList,保存输入的信息,并作为下面调用ADDNEWS的参数;
string newxt4="";
string newnam4="";
if((myFile.PostedFile!=null)&&(myFile.PostedFile.FileName.Trim()!=""))
{
string nam =myFile.PostedFile.FileName;//取得文件名和路径(".")里的索引。
int i=nam.LastIndexOf(".");//取得文件扩展名。
newxt4 =nam.Substring(i);
DateTime now= DateTime.Now;//自动根据日期和文件大小的不同为文件命名,防止文件名重复。
newnam4= now.DayOfYear.ToString()+ myFile.PostedFile.ContentLength.ToString();//获取文件的大小
myFile.PostedFile.SaveAs(Server.MapPath("\\shibing\\UploadFiles\\"+newnam4+newxt4));
} CommAry.Add(System.DateTime.Now);
CommAry.Add(tb_Source.Text.Trim());
CommAry.Add(tb_Title.Text.Trim());
CommAry.Add(tb_Content.Text.Trim());
CommAry.Add(newnam4+newxt4);
try
{
mywebNews.AddNews(CommAry);
tb_Date.Text="";
tb_Source.Text="";
tb_Title.Text="";
tb_Content.Text="";
}
catch
{
Response.Redirect("Error.aspx");
}
} private void btn_Reset_Click(object sender, System.EventArgs e)
{
tb_Date.Text="";
tb_Source.Text="";
tb_Title.Text="";
tb_Content.Text="";
}
}
}

解决方案 »

  1.   

    aspx文件
    <%@ Page language="c#" Codebehind="WriteNews.aspx.cs" AutoEventWireup="false" Inherits="shibing.admin.WriteNews" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>添加新闻</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <LINK href="css/zt.css" type="text/css" rel="stylesheet">
    <style type="text/css">
    BODY { FONT-SIZE: 12px }
    TD { FONT-SIZE: 12px }
    TH { FONT-SIZE: 12px }
    .style2 { FONT-SIZE: 10px }
    .style1 { FONT-SIZE: 12px }
    .style4 { COLOR: #339900 }
    .style44 { FONT-SIZE: 12px; COLOR: #3f5212; LINE-HEIGHT: 16px }
    .style45 { FONT-SIZE: 12px; COLOR: #3f5212 }
    .unnamed1 { BORDER-RIGHT: #00ffff 0px dotted; BORDER-TOP: #00ffff 0px dotted; BORDER-LEFT: #00ffff 0px dotted; BORDER-BOTTOM: #00ffff 0px dotted }
    </style>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" encType="multpart/form-data" runat="server">
    <table width="597" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#009999"
    style="WIDTH: 597px; HEIGHT: 311px">
    <TR>
    <TD width="86"><FONT face="宋体">&nbsp;</FONT></TD>
    <TD width="552"><FONT face="宋体" size="4"><STRONG>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <FONT color="#0000ff">添加新闻</FONT></STRONG></FONT></TD>
    </TR>
    <tr>
    <td width="86"><div align="right" class="style1">标 题:</div>
    </td>
    <td width="552">&nbsp;
    <asp:TextBox id="tb_Title" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*标题一定要填!" Display="Dynamic"
    ControlToValidate="tb_Title"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
    <td><div align="right" class="style1">来 源:</div>
    </td>
    <td>&nbsp;
    <asp:TextBox id="tb_Source" runat="server"></asp:TextBox>
    <FONT color="#000000">*如没可不填!</FONT>&nbsp;<FONT color="#000000">&nbsp;</FONT></td>
    </tr>
    <tr>
    <td><div align="right" class="style1">发布时间:</div>
    </td>
    <td>&nbsp;
    <asp:TextBox id="tb_Date" runat="server"></asp:TextBox>
    *如是转载请注明出处!</td>
    </tr>
    <tr>
    <td><div align="right" class="style1">新闻类别:</div>
    </td>
    <td>&nbsp;
    <asp:DropDownList id="DD_kind" runat="server"></asp:DropDownList></td>
    </tr>
    <tr>
    <td><div align="right" class="style1">图 片:</div>
    </td>
    <td>&nbsp; <INPUT type="file" id="File1" name="File1"></td>
    </tr>
    <tr>
    <td style="HEIGHT: 226px"><div align="right" class="style1">新闻内容:</div>
    </td>
    <td style="HEIGHT: 226px">&nbsp;
    <asp:TextBox id="tb_Content" runat="server" TextMode="MultiLine" Width="472px" Height="213px"></asp:TextBox></td>
    </tr>
    <tr>
    <td><div align="right"><span class="style1"><FONT face="宋体">&nbsp;</FONT></span></div>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><div align="right" class="style1">重点推荐:</div>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td dir="ltr">&nbsp;<FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </FONT>
    <asp:Button id="btn_Replay" runat="server" Text="提 交"></asp:Button>
    <FONT face="宋体">&nbsp;&nbsp;&nbsp; </FONT>
    <asp:Button id="btn_Reset" runat="server" Text="返 回"></asp:Button></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
    News类中的AddNews方法
    public void AddNews(ArrayList tempary)//本函数用于新闻的添加,查找,删除等操作。
    {
    SqlConnection cn=new SqlConnection(strConn);
    SqlCommand cmd=new SqlCommand("AddNews,cn");//下面两句调用名为updatePBaseInfo存储过程
    cmd.CommandType =CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@publicDate",SqlDbType.DateTime));
    cmd.Parameters["@publicDate"].Value =tempary[0];//给存储过程的参数@publicDate赋值。 cmd.Parameters.Add(new SqlParameter("@source",SqlDbType.VarChar,30));
    cmd.Parameters["@source"].Value= tempary[1]; cmd.Parameters.Add(new SqlParameter("@title",SqlDbType.VarChar,100));
    cmd.Parameters["@title"].Value= tempary[2]; cmd.Parameters.Add(new SqlParameter("@content",SqlDbType.Text));
    cmd.Parameters["@content"].Value=tempary[3]; cmd.Parameters.Add(new SqlParameter("@picture",SqlDbType.VarChar,50));
    cmd.Parameters["@picture"].Value=tempary[4]; cmd.Parameters.Add(new SqlParameter("@hits",SqlDbType.Int));
    cmd.Parameters["@hits"].Value=0;
    try 
    {
    cn.Open();
    cmd.ExecuteNonQuery();
    }
    catch(System.Data.SqlClient.SqlException er)
    {
    throw new Exception(er.Message); }
    finally
    {
    cmd.Dispose();
    cn.Close();
    }
    }
    错误的提示:
    行 63:  string newxt4="";
    行 64:  string newnam4="";
    行 65:  if((myFile.PostedFile!=null)&&(myFile.PostedFile.FileName.Trim()!=""))
    行 66:  {
    行 67:  string nam =myFile.PostedFile.FileName;//取得文件名和路径(".")里的索引。