这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class welcome : System.Web.UI.Page
{
string fname;
string fpath;
SqlConnection con = new SqlConnection();
SqlCommand cmd;
string emailname;
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["Username"] == null) && (Session["useraddress"] == null))
{
Response.Redirect("Registration.aspx");
}
else
{
emailname = Session["useremail"].ToString();
Label2.Text = Session["Username"].ToString();
Label3.Text = Session["useraddress"].ToString();
welcomelbl.Text = Session["Username"].ToString();
addlbl.Text = Session["useraddress"].ToString();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("login.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
imageupload();
}
public void imageupload()
{
fpath = FileUpload2.PostedFile.FileName;
if ((string.IsNullOrEmpty(fpath)))
{
return;
}
fname = System.IO.Path.GetFileName(fpath);
FileUpload2.PostedFile.SaveAs(Server.MapPath("~/"+emailname+"/")+fname);
imagepathtext.Text = Server.MapPath("~/"+emailname+"") + fname;
imgnametext.Text = fname;
divimg.Style.Add("background-image", "url("+fname+")");
SqlConnection con = Connection.conn();
con.Open();
SqlCommand cmd = new SqlCommand("insert into imgtbl (imgname,useraddress,imgaddress) values(@name ,'" + emailname + "' ,@imgaddress)", con);
cmd.Parameters.AddWithValue("@name", imgnametext.Text).ToString(); ;
cmd.Parameters.AddWithValue("@imgaddress",imagepathtext.Text).ToString();
cmd.ExecuteNonQuery();
con.Close();
return;
}
}
答案 0 :(得分:0)
Ist Problem Example
由于您的问题,可能会声明列datatype varchar(25)
,但在该列中插入的数据超过25个字符。
检查数据类型长度
对于第二个问题,请像这样指定您的图像路径
string fname = @"C:\imagepath";
fpath = System.IO.Path.GetFileName(fname);
答案 1 :(得分:0)
第二个问题是由于我认为的语法。在函数imageupload中更改代码,如:
divimg.Style.Add(“background”,“url(”+ imagepath +“)”);