Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C3 calling stored Procedure

Status
Not open for further replies.

mm0reira

Programmer
Jun 11, 2008
2
PT
Hi all,

I'm having a litle issue, I've already looked at some examples, but my problem persits. I'm trying to call a insert stored procedure in my C# windows application. but i receive an error message telling me that i'm not passing the parameter.


try
{
using (SqlConnection cnn = new SqlConnection(cnnStr.Text))
{
using (SqlCommand cmd = new SqlCommand("[Files_Insert]", cnn))
{
FileStream fs = File.Open(tbFile.Text, FileMode.Open, FileAccess.Read);
int length = (int)fs.Length + 1;
fs.Close();

cmd.CommandType = CommandType.StoredProcedure;

SqlParameter prm = new SqlParameter("@P_FILE_ID", SqlDbType.UniqueIdentifier, 32);
prm.Direction = ParameterDirection.ReturnValue;
prm.SqlValue = null;
cmd.Parameters.Add(prm);

prm = new SqlParameter("@P_FILE", SqlDbType.VarBinary, length);
prm.SqlValue = File.ReadAllBytes(tbFile.Text);
cmd.Parameters.Add(prm);

cnn.Open();
int i = cmd.ExecuteNonQuery();

}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

and for inserting data into the sql table, i use this stored procedure;

ALTER PROCEDURE [dbo].[Files_Insert](
@P_FILE_ID uniqueidentifier output,
@P_FILE varbinary(MAX)
)
AS
BEGIN
DECLARE @GUID uniqueidentifier
SET @GUID = NEWID()

SET NOCOUNT ON;
INSERT INTO [FILE_VALUE]
(
FILE_ID,
[FILE]
)
VALUES
(
@GUID,
@P_FILE
)
SET @P_FILE_ID = @GUID

END

And the eror message i'm receiving is this one

URL]
 
remove the @ symbol from your paremeters. you also shouldn't have to set the parameter length.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
well the issue wasn't the @symbol at the parameter, here is the correct code, now it's allright and runig! thanks

FileInfo fi = new FileInfo(tbFile.Text);
StreamReader sr = fi.OpenText();
string str = (string)sr.ReadToEnd(); // File Content, this should be Binary data
byte[] stream = ConvertToByte(str, str.Length);

cmd.Parameters.AddWithValue("@p_file_id", new System.Guid()).Direction = ParameterDirection.InputOutput;
cmd.Parameters.AddWithValue("@p_file", stream);

cnn.Open();
cmd.ExecuteNonQuery();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top