Using GUID and SQL in code

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

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(Object sender, GridViewCommandEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "MyAlert2",
"alert('Command=" + e.CommandName + "');", true);
if (e.CommandName == "New")
{
SqlCommand cmd = new SqlCommand();

//First problem How do I make a GUID?
SqlGuid g = new SqlGuid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
//Second problem, how do I use the GUID in the Insert
cmd.CommandText = "INSERT INTO dbo.Table1
Values("+g.ToString()+",'tst')"; //?????


cmd.CommandType = CommandType.Text ;
SqlConnection sqlConnection1 = new
SqlConnection(SqlDataSource1.ConnectionString);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
GridView1.DataBind();
}
}
 
SqlGuid guid = new SqlGuid(Guid.NewGuid());
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! |
********************************************
 
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.NewGuid());
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.Text ;
SqlConnection sqlConnection1 = new
SqlConnection(SqlDataSource1.ConnectionString);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
GridView1.DataBind();



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.SqlClient.SqlException: 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.ExecuteNonQuery();
Line 43: sqlConnection1.Close();
Line 44: GridView1.DataBind();


Source File: c:\Documents and Settings\Marc wentink\Mijn documenten\Visual
Studio 2005\WebSites\H4SP2\Default.aspx.cs Line: 42

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) +95
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) +82
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj) +346
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj) +3244
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString) +186
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
+1121
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method,
DbAsyncResult result) +334
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) +407
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +149
_Default.gv_RowCommand(Object sender, GridViewCommandEventArgs e) in
c:\Documents and Settings\Marc wentink\Mijn documenten\Visual Studio
2005\WebSites\H4SP2\Default.aspx.cs:42
System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String
eventArgument) +215
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838





Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
Version:2.0.50727.42
 
That looks like more of a varchar/string issue, rather than a Guid.

What's the maxlength of your string column in the db?


Marc said:
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.NewGuid());
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.Text ;
SqlConnection sqlConnection1 = new
SqlConnection(SqlDataSource1.ConnectionString);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
GridView1.DataBind();



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.SqlClient.SqlException: 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.ExecuteNonQuery();
Line 43: sqlConnection1.Close();
Line 44: GridView1.DataBind();


Source File: c:\Documents and Settings\Marc wentink\Mijn documenten\Visual
Studio 2005\WebSites\H4SP2\Default.aspx.cs Line: 42

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) +95
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) +82

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj) +346
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj) +3244
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString) +186
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
+1121
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method,
DbAsyncResult result) +334
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) +407
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +149
_Default.gv_RowCommand(Object sender, GridViewCommandEventArgs e) in
c:\Documents and Settings\Marc wentink\Mijn documenten\Visual Studio
2005\WebSites\H4SP2\Default.aspx.cs:42
System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String
eventArgument) +215

System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838





Version Information: Microsoft .NET Framework Version:2.0.50727.42;
ASP.NET Version:2.0.50727.42


Cowboy (Gregory A. Beamer) said:
SqlGuid guid = new SqlGuid(Guid.NewGuid());
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! |
********************************************
 
Back
Top