C# .NET - passing string value to a stored procedure which is type int
Asked By jyothi
01-Mar-10 09:46 AM
end of post
F Cali replied to jyothi
As long as the string value can be converted to an int, you should be able to pass that string value to your stored procedure. Assuming this is for SQL Server, I suggest using SqlParameter with your SqlCommand:
SqlConnection sqlConnection = new SqlConnection("YourConnectionString");
SqlCommand sqlCommand = new SqlCommand("YourSPHere", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Add("@Parameter1", SqlDataType.Int);
sqlCommand.Parameters["@Parameter1"].value = yourStringValueHere;
sqlCommand.ExecuteNonQuery();
Regards,
http://www.sql-server-helper.com/sql-server-2008/merge-statement-with-table-valued-parameters.aspx
[ Kirtan ] replied to jyothi
Friend, you can Add Parameters to Command Object By 2 methods
Command.Parameters.Add() ;
and
Command.Parameters.AddWithValue("@Pram1",ValueHere) ;
both method Does the same task but difference is AddWithValue Takes Input Value as Object and Validated At SQL Server ..
while Add() method Validates it at application Level itself ..
Jonathan VH replied to jyothi
Validate and convert the string in your code, then use the converted value (myInt in this example) to instantiate the parameter, e.g.:
string newString = "123456789";
try
{
int myInt = Convert.ToInt32(newString);
Console.WriteLine(myInt);
}
catch (FormatException)
{
Console.WriteLine("{0} does not represent a number.",
newString);
}
catch (OverflowException)
{
Console.WriteLine("{0} is out of range of the integer type.",
newString);
}
I would simply choose to do this
Go back to the stored procedure and
change the data type of the input parameter that accepts the number as Int.
Do not play with other datatypes. They can cause irrepairable damage.

generic exception handler. According to the MSDN documentation, Convert.ToInt32 only throws ArgumentException , FormatException and OverflowException . So, those are the only exceptions that should be handled. The problem was on our in future articles. • Include SET NOCOUNT ON statement: With every SELECT and DML statement, the SQL server returns a message that indicates the number of affected rows by that statement. This information of returning this extra information. For stored procedures that contain several statements or contain Transact-SQL loops, setting SET NOCOUNT to ON can provide a significant performance boost because network traffic sp_” in the stored procedure name : If a stored procedure name begins with “SP_, ” then SQL server first searches in the master database and then in the current session database. Searching in reuse of the existing complied plan will result in improved performance. • Try to avoid using SQL Server cursors whenever possible: Cursor uses a lot of resources for overhead processing to maintain current
insert phone number in a txt field, i doing it in c# WIN APP, in sql server i used phone.int, in procedure also sqldbtype.int i am using so where is false); if ((style & NumberStyles.AllowHexSpecifier) ! = NumberStyles.None) { if (!HexNumberToInt32(ref number, ref num)) { throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); } return num; } if (!NumberToInt32(ref number, ref num)) { throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); } return num; } Convert.ToInt32("32"): public static int ToInt32(string value) { if it like this way int phoneNo = Convert .ToInt32(txtPhone.Text.ToString()); you are specifiyinf type SQL type integer and passing argument as string that makes problem and error use integer while
Convert RTF to plain text in sql SQL Server Hi All, Can anyone tell me how to convert RTF formatted data into plain text? I have a column in sql server 2005 table which stores the rtf data. I want to convert and store this data another table. So then I can display this data in my SSRS 2005 report. Thanks, SQL Server Discussions System.IO.BinaryReader (1) System.IO.Stream (1) MyBase.ReadBytes (1) ObjError.GetHtmlErrorMessage (1 text + Chr(rtfData(i)) prev = rtfData(i) End If End Select Next Catch of As OverflowException result = "ReadText: " + rptError(of) Catch re As NullReferenceException result = "ReadText: " + rptError(re) Catch ir As
Problem installing on x64 SharePoint Hi, I am given a Windows Server 2003 x64 box and trying to install WSS 3.0 on it. I have successfully light here? Cheers, Dan Failed to create the configuration database. An exception of type System.OverflowException was thrown. Additional exception information: Arithmetic operation resulted in an overflow. System.OverflowException: Arithmetic operation resulted in an overflow. at System.Diagnostics.NtProcessInfoHelper.GetProcessInfos(IntPtr dataPtr) at System 1) Microsoft.SharePoint.Administration.SPFarm.Update (1) SPPersistedObject (1) System.Diagnostics.Process.EnsureState (1) Windows Server 2003 (1) SharePoint (1) SqlConnectionStringBuilder (1) StoreClassIfNecessary (1) Hello! There is no really problem with x64, we install always on x64 machines. Hmmm. Can you tell us more about your server infrastructure. everything on the same server, or multiple servers. wich service accounts are used? cu Nahed check the service accounts and the service account is just a local account with Administrators access. I have setup similar server infrastructure without any problem on 32bit box. I tried to run the PSConfig but still immediately and same error again. Dan hello, we need to know everything. where is the sql server, where the domain controller. how many service accounts are you using? (normaly you need