String or binary data would be truncated. ปัญหาการ post ในเว็บบอร์ดครับ แปลกมากๆ ใครเคยเจอปัญหานี้ช่วยแนะนำด้วยครับ
Error นี้ไม่ได้มีอะไรซับซ้อนเลยครับ คือ จำนวนฟิวด์ไม่พอดีกัลจำนวน Data ครับ หรือว่า Insert ค่าลงผิดประเภทของฟิวด์ครับ
Date :
2011-03-21 16:21:11
By :
webmaster
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace Webboard
{
public partial class Reply : System.Web.UI.Page
{
string CurrentTID = "";
protected void Page_Load(object sender, EventArgs e)
{
CurrentTID = Request.QueryString["TID"];
if (CurrentTID == null)
{
Response.Redirect("Default.aspx");
}
else
{
if (CurrentTID == "")
{
Response.Redirect("Default.aspx");
}
else
{
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["WebboardConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT TID,Description,UserName,RecordDate,ViewCount,ReplyCount");
sb.Append(" FROM Topic");
sb.Append(" WHERE TID=@TID");
var sqlTopic = sb.ToString();
SqlCommand com = new SqlCommand();
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@TID", SqlDbType.NVarChar).Value = CurrentTID;
com.CommandType = CommandType.Text;
com.CommandText = sqlTopic;
var dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
var Description = dr.GetString(dr.GetOrdinal("Description"));
var UserName = dr.GetString(dr.GetOrdinal("UserName"));
var RecordDate = dr.GetDateTime(dr.GetOrdinal("RecordDate"));
var ViewCount = dr.GetInt32(dr.GetOrdinal("ViewCount"));
dr.Close();
sb.Clear();
sb.Append("UPDATE Topic");
sb.Append(" SET ViewCount=@ViewCount");
sb.Append(" WHERE (TID=@TID)");
var sqlUpdate = sb.ToString();
com.Parameters.Clear();
com.Parameters.Add("@ViewCount", SqlDbType.Int).Value = ViewCount + 1;
com.Parameters.Add("@TID", SqlDbType.NVarChar).Value = CurrentTID;
com.CommandText = sqlUpdate;
com.ExecuteNonQuery();
Response.Write("<a href='Default.aspx' font-underline='#false'>กลับสู่เว็บบอร์ด</a><br /><br />");
Response.Write("<table style='width:960px;'border='0'>");
Response.Write("<tr><td colspan='3' bgcolor='#FFCCCC'>" + "หัวข้อกระทู้ : " + Description + "</td></tr>");
Response.Write("<tr><td bgcolor='#FFCCCC'>" + "จากคุณ : " + UserName + "</td>");
Response.Write("<td bgcolor='#FFCCCC'>" + "วันที่ตั้งกระทู้ : " + RecordDate + "</td></tr></table>");
}
dr.Close();
}
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["WebboardConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT TOP 1 ReplyNO FROM Reply");
sb.Append(" WHERE TID=@TID");
sb.Append(" ORDER BY ReplyNO Desc");
var sqlSave = sb.ToString();
int LastReplyNO;
SqlCommand com = new SqlCommand();
SqlDataReader dr;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@TID", SqlDbType.NVarChar).Value = CurrentTID;
com.CommandType = CommandType.Text;
com.CommandText = sqlSave;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
var CurrentReplyNO = dr.GetInt32(0);
CurrentReplyNO++;
LastReplyNO = CurrentReplyNO;
}
else
{
LastReplyNO = 1;
}
dr.Close();
sb.Clear();
sb.Append("INSERT INTO Reply(TID,ReplyNO,Description,ReplyName,ReplyDate,IP)");
sb.Append("VALUES (@TID,@ReplyNo,@Description,@ReplyName,@ReplyDate,@IP)");
sqlSave = sb.ToString();
com.CommandText = sqlSave;
com.Parameters.Clear();
com.Parameters.Add("@TID", SqlDbType.NVarChar).Value = CurrentTID;
com.Parameters.Add("@ReplyNO", SqlDbType.NVarChar).Value = LastReplyNO;
com.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text.Trim();
com.Parameters.Add("@ReplyName", SqlDbType.NVarChar).Value = txtReplyName.Text.Trim();
com.Parameters.Add("@ReplyDate", SqlDbType.DateTime).Value = DateTime.Now;
com.Parameters.Add("@IP", SqlDbType.NVarChar).Value = Request.ServerVariables["REMOTE_ADDR"];
com.ExecuteNonQuery();
sb.Clear();
sb.Append("UPDATE Topic");
sb.Append(" SET ReplyCount=@ReplyCount");
sb.Append(" WHERE (TID=@TID)");
var sqlUpdate = sb.ToString();
com.Parameters.Clear();
com.Parameters.Add("@ReplyCount", SqlDbType.Int).Value = LastReplyNO;
com.Parameters.Add("@TID", SqlDbType.NVarChar).Value = CurrentTID;
com.CommandText = sqlUpdate;
com.ExecuteNonQuery();
Conn.Close();
Response.Redirect("Default.aspx");
}
protected void cmdClear_Click(object sender, EventArgs e)
{
txtDescription.Text = "";
txtReplyName.Text = "";
}
}
}
รบกวนช่วยดู code ให้หน่อยนะครับ ว่ามีข้อผิดพลาดตรงไหนหรือเปล่าครับ พยายามแล้วครับ ยังบันทึกลงฐานข้อมูลไม่ได้เลย
Date :
2011-03-22 11:45:19
By :
kittipoth
Code
Server Error in '/' Application.
String or binary data would be truncated.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +404
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +412
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1363
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6312253
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +6313986
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +538
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +689
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +327
Webboard.Post.cmdSave_Click(Object sender, EventArgs e) in C:\Users\thaivb\Desktop\Chxx Webboard\Webboard\CS\Webboard\Post.aspx.cs:62
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
ข้อความที่ error ตอนตั้งกระทู้นะครับ
ประวัติการแก้ไข 2011-03-22 11:57:02
Date :
2011-03-22 11:56:19
By :
kittipoth
Load balance : Server 04