I am beginning to loose faith now.... I am getting the message:
String or binary data would be truncated.
The statement has been terminated.
This is the code:
SqlCommand cmd = new SqlCommand();
SqlGuid guid = new SqlGuid(Guid.Ne wGuid());
string str = "some string here";
string sql = "INSERT INTO TABLE1 VALUES (@guid,@str)";
cmd.CommandText = sql;
cmd.Parameters. AddWithValue("@ guid", guid);
cmd.Parameters. AddWithValue("@ str", str);
cmd.CommandType = CommandType.Tex t ;
SqlConnection sqlConnection1 = new
SqlConnection(S qlDataSource1.C onnectionString );
cmd.Connection = sqlConnection1;
sqlConnection1. Open();
cmd.ExecuteNonQ uery();
sqlConnection1. Close();
GridView1.DataB ind();
Server Error in '/H4SP2' 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.Sql Client.SqlExcep tion: String or binary data
would be truncated.
The statement has been terminated.
Source Error:
Line 40: cmd.Connection = sqlConnection1;
Line 41: sqlConnection1. Open();
Line 42: cmd.ExecuteNonQ uery();
Line 43: sqlConnection1. Close();
Line 44: GridView1.DataB ind();
Source File: c:\Documents and Settings\Marc wentink\Mijn documenten\Visu al
Studio 2005\WebSites\H 4SP2\Default.as px.cs Line: 42
Stack Trace:
[SqlException (0x80131904): String or binary data would be truncated.
The statement has been terminated.]
System.Data.Sql Client.SqlConne ction.OnError(S qlException exception,
Boolean breakConnection ) +95
System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion
exception, Boolean breakConnection ) +82
System.Data.Sql Client.TdsParse r.ThrowExceptio nAndWarning(Tds ParserStateObje ct
stateObj) +346
System.Data.Sql Client.TdsParse r.Run(RunBehavi or runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleR esultSet
bulkCopyHandler , TdsParserStateO bject stateObj) +3244
System.Data.Sql Client.SqlComma nd.FinishExecut eReader(SqlData Reader ds,
RunBehavior runBehavior, String resetOptionsStr ing) +186
System.Data.Sql Client.SqlComma nd.RunExecuteRe aderTds(Command Behavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
+1121
System.Data.Sql Client.SqlComma nd.RunExecuteRe ader(CommandBeh avior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method,
DbAsyncResult result) +334
System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult
result, String methodName, Boolean sendToPipe) +407
System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() +149
_Default.gv_Row Command(Object sender, GridViewCommand EventArgs e) in
c:\Documents and Settings\Marc wentink\Mijn documenten\Visu al Studio
2005\WebSites\H 4SP2\Default.as px.cs:42
System.Web.UI.W ebControls.Grid View.OnRowComma nd(GridViewComm andEventArgs
e) +96
System.Web.UI.W ebControls.Grid View.HandleEven t(EventArgs e, Boolean
causesValidatio n, String validationGroup ) +121
System.Web.UI.W ebControls.Grid View.RaisePostB ackEvent(String
eventArgument) +215
System.Web.UI.W ebControls.Grid View.System.Web .UI.IPostBackEv entHandler.Rais ePostBackEvent( String
eventArgument) +31
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +244
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +3838
Version Information: Microsoft .NET Framework Version:2.0.507 27.42; ASP.NET
Version:2.0.507 27.42
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMschreef in
bericht news:uN******** ******@TK2MSFTN GP06.phx.gbl...
SqlGuid guid = new SqlGuid(Guid.Ne wGuid());
string str = "some string here";
string sql = "INSERT INTO TABLE VALUES (@guid,@str)";
SqlCommand cmd = new SqlCommand();
cmd.Parameters. AddWithValue("@ guid", guid);
cmd.Parameters. AddWithValue("@ str", str);
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
*************** *************** **************
| Think outside the box! |
*************** *************** **************
"Marc" <no*****@chello .nlwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Hi, I don't get it I cannot get this to work, can somebody give me a hint
Table1 contains a field Id which is a GUID as primary key and DATA a
string, I want to insert a new row but it does not work.
void gv_RowCommand(O bject sender, GridViewCommand EventArgs e)
{
ClientScript.Re gisterStartupSc ript(GetType(), "MyAlert2",
"alert('Comman d=" + e.CommandName + "');", true);
if (e.CommandName == "New")
{
SqlCommand cmd = new SqlCommand();
//First problem How do I make a GUID?
SqlGuid g = new SqlGuid("3AAAAA AA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
//Second problem, how do I use the GUID in the Insert
cmd.CommandText = "INSERT INTO dbo.Table1
Values("+g.ToS tring()+",'tst' )"; //?????
cmd.CommandType = CommandType.Tex t ;
SqlConnection sqlConnection1 = new
SqlConnection( SqlDataSource1. ConnectionStrin g);
cmd.Connection = sqlConnection1;
sqlConnection1. Open();
cmd.ExecuteNonQ uery();
sqlConnection1. Close();
GridView1.DataB ind();
}
}