Using GUID and SQL in code

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();
}
}
 
C

Cowboy \(Gregory A. Beamer\)

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);
 
M

Marc

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
 
S

sloan

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top