Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Single Char insert with ADO.NET

Author  Topic 

robbinsb
Starting Member

4 Posts

Posted - 2005-03-18 : 14:40:06
I am trying to insert to a table that has a column defined as:
SourceType  char(1)  NOT NULL

using a stored proc in C#. I have the class attribute defined as:
char  source_type;

In my code, I modify the property to contain a capital letter, and I define an SQLCommand to add the parameter as follows:

cmd.Parameters.Add(
new System.Data.SqlClient.SqlParameter("@SourceType", System.Data.SqlDbType.Char, 1));
cmd.Parameters["@SourceType"].Value = s.SourceType;

(where 's' is my instantiated class object, and SourceType is the property referencing the attribute.)

When I execute the SQLCommand to assign the value to the parameter, I get the following error:

"The parameter data type of Char is invalid."

If I change the class attribute to a string datatype, and then execute the command, I get this error:

String or binary data will be truncated.
The statement has been terminated.


What I am missing?

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-18 : 15:03:50
The character string You are trying to insert is more than 1 character long.
You could change your class attribute to char iof string,
or make sure that the string is just 1 char long,
or just insert the leftmost character s.SourceType.Substring(0,1)



rockmoose
Go to Top of Page

robbinsb
Starting Member

4 Posts

Posted - 2005-03-18 : 15:39:29
rockmoose - thanks for the suggestion.

I did try the Substring, but I still get the Truncation error message.

Have you (or anyone else) tried to insert into a column defined with 1 char in .NET?

Bruce
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-18 : 16:11:57
Use profiler and start a trace to see what is going on.
Profiler is a great tool to track the commands that clients send to the db.

rockmoose
Go to Top of Page

robbinsb
Starting Member

4 Posts

Posted - 2005-03-18 : 16:31:21
rockmoose - using SQL Profiler would imply that my stored proc actually executes on the server; I believe that it doesn't make it that far. I think that this is a coding issue for which I am cannot seem to find a suitable solution.
Plus, you need Sysadmin authority to run Profiler (which I do not have at this time.)

Bruce
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-03-18 : 16:40:50
quote:
Originally posted by robbinsb

rockmoose - using SQL Profiler would imply that my stored proc actually executes on the server; I believe that it doesn't make it that far. I think that this is a coding issue for which I am cannot seem to find a suitable solution.
Plus, you need Sysadmin authority to run Profiler (which I do not have at this time.)

Bruce



Set a breakpoint in your code ?

Get admin privelages ?

Call an admin ?

TGIF


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-18 : 17:25:25
The second error message that You showed ("String or binary data will be truncated....") looks like SQL Server, thus the profiler, but ok.

try splitting the code and step through it, to see exactly where the trouble is
SqlParameter prm = new SqlParameter("@SourceType", SqlDbType.Char, 1);
prm.Value = s.SourceType;
cmd.Parameters.Add(prm);
...
cmd.Execute...



rockmoose
Go to Top of Page

robbinsb
Starting Member

4 Posts

Posted - 2005-03-21 : 09:30:16
Okay, in my frustration, I rewrote the code with a fresh start, and discovered a missing parameter (should have caught in the first place, but I did not have control over the initial writing) that was the reason for the trucation... (Doh!)

It works as expected now.

Thanks for your help and for the prompt responses.

Bruce
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-21 : 09:43:21
>> I rewrote the code with a fresh start
Sometimes a good approach

rockmoose
Go to Top of Page
   

- Advertisement -