我已经准备好了这个表格来上传一本书到服务器端数据库及其图像。
我必须在另一页上阅读和检索这本书,从而将图像路径保存在数据库中,并将图像保存在“上传”文件夹中。
我试过调试代码,问题是调试箭头甚至没有进入按钮单击事件。
在设计部分,只有一个简单的表单,包括检索客户端图书信息的文本框和文件上传控制器,在相同的按钮单击事件中。
public partial class UploadBooks : System.Web.UI.Page
{
string strcon = WebConfigurationManager.ConnectionStrings["StudentConnectionString1"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
// add session name
//Label3.Text = Session["StudFirstName"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
// image uploading
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload1.PostedFile.ContentLength < 102400)
{
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Uploads") + filename);
Label2.Text = "Upload status: File uploaded!";
}
else
Label2.Text = "Upload status: The file has to be less than 100 kb!";
}
else
Label2.Text = "Upload status: Only JPEG files are accepted!";
}
catch (Exception ex)
{
Label2.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Insert into Books (StudId,BookId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@sid,@bid,@t,@a,@d,@p,@o,@n,@i)", con);
cmd.Parameters.AddWithValue("@sid", Label4.Text);
cmd.Parameters.AddWithValue("@bid", Label1.Text);
cmd.Parameters.AddWithValue("@t", TextBox1.Text);
cmd.Parameters.AddWithValue("@a", TextBox2.Text);
cmd.Parameters.AddWithValue("@d", TextBox3.Text);
cmd.Parameters.AddWithValue("@p", TextBox6.Text);
cmd.Parameters.AddWithValue("@o", TextBox4.Text);
cmd.Parameters.AddWithValue("@n", TextBox5.Text);
cmd.Parameters.AddWithValue("@i", FileUpload1.FileName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("1 Book Added");
}
}
}发布于 2014-03-28 21:30:19
根据您在注释中的说明,很明显您正在尝试将值插入到表的IDENTITY列中。
很可能是StudId或BookID列。
从cmd.Parameters.AddWithValue()语句和INSERT字符串中删除identity列,您应该会做得很好。
SQL会自动插入此值并根据以前的值进行增量。
如果您的标识列是StudId,那么插入字符串应该如下所示:
"Insert into Books (BookId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@bid,@t,@a,@d,@p,@o,@n,@i)"如果您的标识列是BookId,那么它应该如下所示:
"Insert into Books (StudId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@sid,@t,@a,@d,@p,@o,@n,@i)"然后删除相关参数的cmd.Parameters.AddWithValue()行。
https://stackoverflow.com/questions/22723048
复制相似问题