By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Using GUID and SQL in code

P: n/a
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();
}
}
Aug 12 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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! |
********************************************
"Marc" <no*****@chello.nlwrote in message
news:%2****************@TK2MSFTNGP03.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(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();
}
}
Aug 12 '08 #2

P: n/a
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(SqlExc eption exception,
Boolean breakConnection) +95
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection) +82
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj) +346
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj) +3244
System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString) +186
System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(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.InternalExecuteNo nQuery(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(Gr idViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.RaisePostBackEv ent(String
eventArgument) +215
System.Web.UI.WebControls.GridView.System.Web.UI.I PostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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)" <No************@comcast.netNoSpamMschreef in
bericht news:uN**************@TK2MSFTNGP06.phx.gbl...
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! |
********************************************
"Marc" <no*****@chello.nlwrote in message
news:%2****************@TK2MSFTNGP03.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(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();
}
}

Aug 12 '08 #3

P: n/a
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" <no*****@chello.nlwrote in message
news:eQ**************@TK2MSFTNGP04.phx.gbl...
>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(SqlExc eption exception,
Boolean breakConnection) +95
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection) +82

System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj) +346
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj) +3244
System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString) +186
System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(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.InternalExecuteNo nQuery(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(Gr idViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.RaisePostBackEv ent(String
eventArgument) +215

System.Web.UI.WebControls.GridView.System.Web.UI.I PostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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)" <No************@comcast.netNoSpamMschreef
in bericht news:uN**************@TK2MSFTNGP06.phx.gbl...
> 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! |
********************************************
"Marc" <no*****@chello.nlwrote in message
news:%2****************@TK2MSFTNGP03.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(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();
}
}


Aug 12 '08 #4

P: n/a

"sloan" <sl***@ipass.netschreef
What's the maxlength of your string column in the db?
That's it! Works like a charm now. :-)
Aug 12 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.