473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using GUID and SQL in code

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
4 5491
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: gokul | last post by:
Hi, Iam a newbie to dotnet and I experience problems in using the Browser control in VB .net. Though Iam able to use it with its basic features, I need to customise it. ...
0
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with...
1
by: Henning Kristensen | last post by:
Hi I need a little help with some VB.Net code.. I am pulling my computer's GUID out using WMI Dim wmiObj As New System.Management.ManagementObjectSearcher("SELECT UUID FROM...
1
by: Wm. Scott Miller | last post by:
I have a Custom DTS Task for SQL Server and I've got it working fine, except for when I try to access its properties from an ActiveX script. I have added a new property called Length and a method...
2
by: Anders Borum [.NET/C# MCP] | last post by:
Hello! With C# 2.0 coming up, I was wondering what your thoughts are regarding the introduction of access modifiers on set accessors. Personally, I like the first example, because of its...
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
16
by: Asaf | last post by:
I am trying to create and use a COM object with C#.NET 2005. The assembly is set to "Register for COM interop" but when I am trying to call it from VB on Word 2003 I am getting this error: ...
2
by: sk.rasheedfarhan | last post by:
Hi , I have to extract a xml file line by line. Here is my xml Step1: <category name= "name" guid="{guid}"state="enabled"> Step2: <rules> Step3: <rule name="rule name" guid="{guid}"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.