C# 字符串或二进制数据将被截断。声明已终止。上载配置文件时

C# 字符串或二进制数据将被截断。声明已终止。上载配置文件时,c#,asp.net,image,visual-studio-2013,profile,C#,Asp.net,Image,Visual Studio 2013,Profile,我正在尝试使用asp.net配置文件功能来存储用户信息。我在配置文件属性中有一个图像 我使用的是Visual Studio中asp.net提供的默认配置文件提供程序 下面是web.config中属性的定义 <properties> <add name="FirstName"/> <add name="MiddleName"/> <add name="LastName"/> <add name="Profil

我正在尝试使用asp.net配置文件功能来存储用户信息。我在配置文件属性中有一个图像

我使用的是Visual Studio中asp.net提供的默认配置文件提供程序

下面是web.config中属性的定义

  <properties>
    <add name="FirstName"/>
    <add name="MiddleName"/>
    <add name="LastName"/>
    <add name="ProfileImage" type="System.Byte[]" defaultValue='null'/>
    <add name="MobileNumber"/>
    <add name="TelephoneNumber"/>
  </properties>

检查要将数据插入的数据库列。为数据定义的长度小于要插入的长度


例如,如果您尝试向定义为Firstname Varchar(50)的列插入一个值,并且该值的长度超过50,则这将产生与您提到的相同的异常。

问题的解决方案可能来自SQL Server的结构。
我建议您将SQL表中图像字段的类型从
binary
或任何字段更改为
image

首先,您不应将图像数据类型与ntext一起使用,并且文本数据类型将在Microsoft SQL Server的未来版本中删除。如果你必须用它,那就好了。图像数据类型具有从0到2^31-1(2147483647)字节的可变长度二进制数据。所以它确实有一个限制。因为这个错误是针对一个34kb的文件而来的,不应该是这样的。我不会使用图像数据类型。但我也尝试过使用VarBinary(MAX),但它给出了相同的异常…发布更新数据库的代码。错误来自那里。我不会更新它。正如我提到的,我正在使用asp.net的配置文件功能。。。类Http.Context.Current.Profile有自己的更新函数,我没有修改过。那就是我没有超载。我应该这么做吗?你有没有试过不带个人资料图像。可能是另一列引发了异常。尝试先更新FirstName,然后逐个添加其他列。查看引发错误的内容并检查该错误的列定义。
protected void Button1_Click(object sender, EventArgs e)
        {
            var profile = HttpContext.Current.Profile;
            profile.SetPropertyValue("FirstName", TextBox1.Text);
            profile.SetPropertyValue("LastName",TextBox3.Text);
            profile.SetPropertyValue("MiddleName", TextBox2.Text);
            profile.SetPropertyValue("MobileNumber", TextBox4.Text);
            profile.SetPropertyValue("TelephoneNumber", TextBox5.Text);

        if (IsPostBack)
        {
            Boolean fileok = false;
            String path = Server.MapPath("~/UploadedImages/");
            path = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(path);

            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedextensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedextensions.Length; i++)
                {
                    if (fileExtension == allowedextensions[i])
                    {
                        fileok = true;
                    }
                }
            }
            if (fileok)
            {
                byte[] userImage = new byte[1025];

                try
                {
                   userImage = ReadFully(FileUpload1.PostedFile.InputStream);


                }
                catch (Exception ex)
                {
                    //exception while getting the file
                    return;
                }

                profile.SetPropertyValue("ProfileImage", userImage);
            }
            else
            {
              //file not okay type not image
            }
        }


    }
//-------------------------------------------------------------Get The File in Byte Stream ---------------------------------//
    public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[input.Length];

        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352418
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +269
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1406
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +205
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +160
   System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) +535
   System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +262

[UpdateException: An error occurred while updating the entries. See the inner exception for details.]
   System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +444
   System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) +146
   System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +571
   System.Web.Providers.DefaultProfileProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) +745
   System.Configuration.SettingsBase.SaveCore() +389
   System.Configuration.SettingsBase.Save() +114
   System.Web.Profile.ProfileBase.SaveWithAssert() +31
   System.Web.Profile.ProfileBase.Save() +72
   System.Web.Profile.ProfileModule.OnLeave(Object source, EventArgs eventArgs) +9497686
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69