简体   繁体   English

将imagepath保存在数据库中并更改背景图像

[英]saving imagepath in database and changing background image

  1. First problem .I want to save image path in database but we have an error "String or binary data would be truncated.The statement has been terminated". 第一个问题。我想在数据库中保存图像路径,但出现错误“字符串或二进制数据将被截断。该语句已终止”。
  2. Second problem is that background image is not changing. 第二个问题是背景图像没有改变。

Here is my code: 这是我的代码:

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;
}
}

Ist Problem Example 第一个问题Example

This problem because of you may declared column datatype varchar(25) but while inserting data more than 25 characters in that column. 由于您可能会声明此列datatype varchar(25)但在该列中插入的数据超过25个字符时会出现此问题。

Check your datatype length 检查您的数据类型长度

For second problem specify your image path like this 对于第二个问题,请指定您的图像路径,如下所示

string fname = @"C:\imagepath";
fpath = System.IO.Path.GetFileName(fname);
  1. 1st Problem is due to short length of the datatype in database then data. 第一个问题是由于数据库中的数据类型比数据短。 Change your datatype accordingly. 相应地更改数据类型。
  2. 2nd problem is due to syntax I think. 第二个问题是由于我认为语法。 Change your code in function imageupload like: 在函数imageupload中更改代码,如下所示:

    divimg.Style.Add("background", "url("+imagepath+")"); divimg.Style.Add(“ background”,“ url(” + imagepath +“)”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM