代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class AddSpots : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = "请添加景点信息!";
} }
protected void TextBox2_TextChanged(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string[] spotsurl = new string[5];
if (FileUpload1.PostedFile.FileName == "" || FileUpload2.PostedFile.FileName == "" || FileUpload3.PostedFile.FileName == "" || FileUpload4.PostedFile.FileName == "" || FileUpload5.PostedFile.FileName == "")
{
this.Label1.Text = "请至少选择一个选择上传景点图片的地址!";
}
else
{
HttpFileCollection myfiles = Request.Files;
for (int i = 0; i < myfiles.Count; i++)
{
HttpPostedFile mypost = myfiles[i];
try
{
if (mypost.ContentLength > 0)
{
string uploadName = mypost.FileName;
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);
string pictureName = DateTime.Now.Ticks.ToString() + i.ToString() + suffix;//把参数i加上去 防止同一时间内图片重复,因为上传的是多幅图片.
string serverpath = Server.MapPath("~/uploadfile1/") + pictureName;
mypost.SaveAs(serverpath);
spotsurl[i] = "uploadfile1/" + pictureName;
}
}
catch (Exception ex)
{
this.Label1.Text = "上传发生错误!原因:" + ex.Message.ToString(); } string selectedarea = this.DropDownList1.SelectedItem.Value;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand Mycmd = new SqlCommand("insert into Scenic-Spots(name,discribe,imgurl1,imgurl2,imgurl3,imgurl4,imgurl5,area) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + spotsurl[0].ToString() + "','" + spotsurl[1].ToString() + "','" + spotsurl[2].ToString() + "','" + spotsurl[3].ToString() + "','" + spotsurl[4].ToString() + "','" + selectedarea.ToString() + "')", conn);
Mycmd.ExecuteNonQuery();
conn.Close();
}
}
this.Label1.Text = spotsurl[0].ToString(); }
}错误如下:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 60: SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
行 61: conn.Open();
行 62: SqlCommand Mycmd = new SqlCommand("insert into Scenic-Spots(name,discribe,imgurl1,imgurl2,imgurl3,imgurl4,imgurl5,area) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + spotsurl[0].ToString() + "','" + spotsurl[1].ToString() + "','" + spotsurl[2].ToString() + "','" + spotsurl[3].ToString() + "','" + spotsurl[4].ToString() + "','" + selectedarea.ToString() + "')", conn);
行 63: Mycmd.ExecuteNonQuery();行 64: conn.Close();
我也百度了好长时间,刚开始学ASP.NET,搜不出来才上来问的,麻烦大家了!
没分了,就剩18分了.......
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class AddSpots : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = "请添加景点信息!";
} }
protected void TextBox2_TextChanged(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string[] spotsurl = new string[5];
if (FileUpload1.PostedFile.FileName == "" || FileUpload2.PostedFile.FileName == "" || FileUpload3.PostedFile.FileName == "" || FileUpload4.PostedFile.FileName == "" || FileUpload5.PostedFile.FileName == "")
{
this.Label1.Text = "请至少选择一个选择上传景点图片的地址!";
}
else
{
HttpFileCollection myfiles = Request.Files;
for (int i = 0; i < myfiles.Count; i++)
{
HttpPostedFile mypost = myfiles[i];
try
{
if (mypost.ContentLength > 0)
{
string uploadName = mypost.FileName;
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);
string pictureName = DateTime.Now.Ticks.ToString() + i.ToString() + suffix;//把参数i加上去 防止同一时间内图片重复,因为上传的是多幅图片.
string serverpath = Server.MapPath("~/uploadfile1/") + pictureName;
mypost.SaveAs(serverpath);
spotsurl[i] = "uploadfile1/" + pictureName;
}
}
catch (Exception ex)
{
this.Label1.Text = "上传发生错误!原因:" + ex.Message.ToString(); } string selectedarea = this.DropDownList1.SelectedItem.Value;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand Mycmd = new SqlCommand("insert into Scenic-Spots(name,discribe,imgurl1,imgurl2,imgurl3,imgurl4,imgurl5,area) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + spotsurl[0].ToString() + "','" + spotsurl[1].ToString() + "','" + spotsurl[2].ToString() + "','" + spotsurl[3].ToString() + "','" + spotsurl[4].ToString() + "','" + selectedarea.ToString() + "')", conn);
Mycmd.ExecuteNonQuery();
conn.Close();
}
}
this.Label1.Text = spotsurl[0].ToString(); }
}错误如下:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 60: SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
行 61: conn.Open();
行 62: SqlCommand Mycmd = new SqlCommand("insert into Scenic-Spots(name,discribe,imgurl1,imgurl2,imgurl3,imgurl4,imgurl5,area) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + spotsurl[0].ToString() + "','" + spotsurl[1].ToString() + "','" + spotsurl[2].ToString() + "','" + spotsurl[3].ToString() + "','" + spotsurl[4].ToString() + "','" + selectedarea.ToString() + "')", conn);
行 63: Mycmd.ExecuteNonQuery();行 64: conn.Close();
我也百度了好长时间,刚开始学ASP.NET,搜不出来才上来问的,麻烦大家了!
没分了,就剩18分了.......
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货