I have an update stored procedure which works perfectly on my sql , yet when I try to update on my c# form it doesn't update , yet my code seems to be right , im not sure why cant I perform an update
this is my update stored procedure
sql script
USE [MediaPlayer]
GO
/* Object: StoredProcedure [dbo].[updateBooks] Script Date: 11/25/2013 07:51:33 */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[updateBooks]
-- Add the parameters for the stored procedure here
#Name nvarchar (50),
#FileName nvarchar(50),
#FilePath nvarchar(50),
#FileSize float,
#DateAdded date,
#MediaLength nvarchar (50),
#MediaSubType nvarchar (50),
#MediaType nvarchar (50),
#Thumbnail image,
#DateAquired datetime,
#BooksName nvarchar (50),
#Publisher nvarchar(50),
#Author nvarchar(50),
#YearOfPublication date,
#Genre nvarchar (50),
#ISBN nvarchar (50),
#Synoposis nvarchar(max),
#SeriesTitle nvarchar(50),
#SeriesNumber nvarchar (50),
#BookCover image,
#GeneralID int output,
#BookID int output
AS
BEGIN
update dbo.Book
SET
BooksName=#BooksName,
Publisher=#Publisher,
Author =#Author,
[Year of publication] =#YearOfPublication,
Genre =#Genre,
ISBN=#ISBN,
Synoposis=#Synoposis,
[Series Title]= #SeriesTitle,
[Series Number] =#SeriesNumber,
[Book Cover] =#BookCover
from Book
Where BookID = #BookID
select ##ROWCOUNT
update dbo.General
SET
Name =#Name,
FileName= #FileName,
FilePath= #FilePath,
FileSize=#FileSize,
DateAdded= #DateAdded,
MediaLength =#MediaLength,
MediaSubType = #MediaSubType,
MediaType = #MediaType,
Thumbnail =#Thumbnail,
DateAquired= #DateAquired
where GeneralID = #GeneralID
END
C# code
private void DoUpdate()
{
try
{
string picLoc = "C:\\Users\\Dee\\Pictures\\PIC\\download.jpg";
try
{
byte[] img = null;
FileStream fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
string connectionString = "Data Source=(local);Initial Catalog=MediaPlayer;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("dbo.updateBooks", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Name", SqlDbType.NVarChar).Value = NametextBox.Text.ToString();
cmd.Parameters.AddWithValue("#FileName", SqlDbType.NVarChar).Value = FileNametextBox.Text.ToString();
cmd.Parameters.AddWithValue("#FileSize", SqlDbType.Float).Value = Convert.ToDouble(fileSizetextBox.Text);
cmd.Parameters.AddWithValue("#FilePath", SqlDbType.NVarChar).Value = FilePathtextBox.Text.ToString();
cmd.Parameters.AddWithValue("#DateAdded", SqlDbType.Date).Value = DateAddeddateTimePicker.Text.ToString();
cmd.Parameters.AddWithValue("#MediaLength", SqlDbType.NVarChar).Value = MediaLengthtetextBox.Text.ToString();
cmd.Parameters.AddWithValue("#MediaSubType", SqlDbType.NVarChar).Value = MediaSubtypetextBox.Text.ToString();
cmd.Parameters.AddWithValue("#MediaType", SqlDbType.NVarChar).Value = MediaTypetextBox.Text.ToString();
cmd.Parameters.Add(new SqlParameter("#Thumbnail", img));
cmd.Parameters.AddWithValue("#DateAquired", SqlDbType.DateTime).Value = DateAquiredDatetimepicker.Text.ToString();
cmd.Parameters.AddWithValue("#Author", SqlDbType.NVarChar).Value = AuthortextBox.Text.ToString();
cmd.Parameters.AddWithValue("#Publisher", SqlDbType.NVarChar).Value = PublishertextBox.Text.ToString();
cmd.Parameters.AddWithValue("#BooksName", SqlDbType.NVarChar).Value = BooksNametextBox.Text.ToString();
cmd.Parameters.AddWithValue("#SeriesTitle", SqlDbType.Date).Value = SeriesTitletextBox.Text.ToString();
cmd.Parameters.AddWithValue("#SeriesNumber", SqlDbType.NVarChar).Value = SeriesNumberTextBox.Text.ToString();
cmd.Parameters.AddWithValue("#Genre", SqlDbType.NVarChar).Value = genretextBox.Text.ToString();
cmd.Parameters.AddWithValue("#Synoposis", SqlDbType.NVarChar).Value = SynoposistextBox.Text.ToString();
cmd.Parameters.AddWithValue("#YearOfPublication", SqlDbType.NVarChar).Value = YearOfPublicationdatetimepicker.Text.ToString();
cmd.Parameters.AddWithValue("#ISBN ", SqlDbType.NVarChar).Value = ISBNtextBox.Text.ToString();
cmd.Parameters.Add(new SqlParameter("#BookCover", img));
SqlParameter parm1 = new SqlParameter("#GeneralID", SqlDbType.Int);
parm1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm1);
SqlParameter parm = new SqlParameter("#BookID", SqlDbType.Int);
parm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm);
//cmd.ExecuteNonQuery();
MessageBox.Show(cmd.ExecuteNonQuery().ToString() + " record(s) Updated.");
// MessageBox.Show("record successfully updated!");
//clrtxtb();
conn.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentException ex)
{
MessageBox.Show("error is " + ex.Message);
}
}
finally
{ }
}
enter code here
What about first uncommenting the line cmd.ExecuteNonQuery(); ?
hi have you tried something like this
SqlConnection sqlConnection = new SqlConnection();
SqlCommand sqlCommand = new SqlCommand();
sqlConnection.ConnectionString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=True";
sqlConnection.Open();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "SPName";
sqlCommand.Parameters.Add("#param1", SqlDbType.VarChar).Value = value1;
sqlCommand.Parameters.Add("#param2", SqlDbType.VarChar).Value = value2;
sqlCommand.Parameters.Add("#Param3", SqlDbType.VarChar).Value = value3;
sqlCommand.ExecuteNonQuery();
The two WHERE clauses in stored procedures use the parameters #BookID and #GeneralID.
Because of this, both are supposed to contain a value when the stored procedure is called.
Instead, no value is passed to those parameters. Furthermore, both are declared as OUTPUT.
That's the reason why nothing is updated.
Related
When using this code, I get an error
Procedure or function 'Registration' expects parameter '#qUsername', which was not supplied
Can someone please tell me how to fix this? This is my controller code and the SQL query for registration
public ActionResult Create([Bind(Include = "UserID,Username,FirstName,LastName,Email,Password,Number,IsAdmin,Salt")] UsersTable usersTable)
{
if (ModelState.IsValid)
{
Int32 rowsAffected;
SqlCommand cmd = new SqlCommand();
SqlConnection sqlConnection1 = new SqlConnection(#"xxx");
cmd.CommandText = "dbo.Registration";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
rowsAffected = cmd.ExecuteNonQuery();
sqlConnection1.Close();
return RedirectToAction("Index");
}
return View(usersTable);
}
Procedure code:
CREATE PROCEDURE dbo.Registration
#qUsername NVARCHAR(50),
#qPassword NVARCHAR(50),
#qFirstName NVARCHAR(40),
#qLastName NVARCHAR(40),
#errorResponse NVARCHAR(250) OUTPUT
AS
BEGIN
SET NOCOUNT ON
DECLARE #salt UNIQUEIDENTIFIER = NEWID()
BEGIN TRY
INSERT INTO dbo.UsersTable (UserID, Password, Salt, FirstName, LastName)
VALUES (#qUsername, HASHBYTES('SHA2_512', #qPassword + CAST(#salt AS NVARCHAR(36))), #salt, #qFirstName, #qLastName)
SET #errorResponse = 'Success'
END TRY
BEGIN CATCH
SET #errorResponse = ERROR_MESSAGE()
END CATCH
END
You need to define and set the parameters for the stored procedure before your call it from your C# code - like this:
cmd.CommandText = "dbo.Registration";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
// define and set parameters!
cmd.Parameters.Add("#qUsername", SqlDbType.NVarChar, 50).Value = .....;
cmd.Parameters.Add("#qPassword", SqlDbType.NVarChar, 50).Value = .....;
cmd.Parameters.Add("#qFirstName", SqlDbType.NVarChar, 40).Value = .....;
cmd.Parameters.Add("#qLastName", SqlDbType.NVarChar, 40).Value = .....;
cmd.Parameters.Add("#errorResponse", SqlDbType.NVarChar, 250).Direction = ParameterDirection.Output;
sqlConnection1.Open();
rowsAffected = cmd.ExecuteNonQuery();
sqlConnection1.Close();
Use the following:
cmd.Parameters.AddWithValue("#qUsername", "xxxxx");
For all parameters before calling:
cmd.ExecuteNonQuery();
enter image description hereenter image description herei am facing a problem , i have posted the stored procedure,the c# code and the error that is occurring while inserting the data into the database. I need a solution of it
string dataString = #"Data Source=DESKTOP-E5V2JHG;Initial catalog=hospital_management;Integrated Security=SSPI";
SqlConnection connect = new SqlConnection(dataString);
connect.Open();
SqlCommand cmd = new SqlCommand("Add_doctor", connect);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("#ContactNo", SqlDbType.Decimal).Value = Convert.ToDecimal(textBox3.Text);
cmd.Parameters.Add("#EmailId", SqlDbType.VarChar).Value = textBox4.Text;
cmd.Parameters.Add("#GenderId", SqlDbType.Int).Value = Convert.ToInt32(comboBox1.SelectedText);
cmd.Parameters.Add("#Nationality", SqlDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("#salary", SqlDbType.Int).Value = Convert.ToInt32(textBox7.Text);
cmd.Parameters.Add("#DOB", SqlDbType.DateTime2).Value = dateTimePicker1;
cmd.Parameters.Add("#shiftId", SqlDbType.Int).Value = Convert.ToInt32(comboBox3.SelectedText);
cmd.Parameters.Add("#qualification", SqlDbType.VarChar).Value = textBox9.Text;
cmd.Parameters.Add("#Address", SqlDbType.VarChar).Value = textBox10.Text;
cmd.Parameters.Add("#HireDate", SqlDbType.DateTime2).Value = dateTimePicker2;
cmd.Parameters.Add("#Pass", SqlDbType.VarChar).Value = textBox8.Text;
cmd.Parameters.Add("#Religion", SqlDbType.VarChar).Value = textBox6.Text;
cmd.Parameters.Add("#specId", SqlDbType.Int).Value = Convert.ToInt32(comboBox2.SelectedText);
cmd.ExecuteNonQuery();
MessageBox.Show("Command Executed");
connect.Close();
this is in c#
Create Proc AddDoctor
#EMPtypeID VARCHAR(50),
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#ContactNo INT,
#EmailId VARCHAR(50),
#GenderId INT,
#Nationality VARCHAR(50),
#salary INT,
#DOB DATETIME2,
#shiftID INT,
#qualification VARCHAR(50),
#Address VARCHAR(50),
#HireDate DATETIME2,
#Pass VARCHAR(50),
#Religion VARCHAR(50),
#specId INT
AS
BeGIN
insert into EMPLOYEES (_EMPtypeID,_FirstName,_LastName,_ContactNo,_EmailId,
_GenderId,_Nationality,_salary,_DOB,_ShiftId,_qualification,
_Address,_HireDate,_Password,_Religion,_specialityId)
values (#EMPtypeID,#FirstName,#LastName,#ContactNo,#EmailId,
#GenderId,#Nationality,#salary,#DOB,#shiftID,#qualification,
#Address,#HireDate,#Pass,#Religion,#specId);
END
this is the stored procedure in the database
In your DOB and HireDate is the problem.
Change this
cmd.Parameters.Add("#DOB", SqlDbType.DateTime2).Value = dateTimePicker1;
cmd.Parameters.Add("#HireDate", SqlDbType.DateTime2).Value = dateTimePicker2;
To this
cmd.Parameters.Add("#DOB", SqlDbType.DateTime2).Value = dateTimePicker1.Text.ToString();
cmd.Parameters.Add("#HireDate", SqlDbType.DateTime2).Value = dateTimePicker2.Text.ToString();
Because there is no input receiving in that 2 table, like an abstract figure of the date. So you need to make it a concrete string. Good for you SQL Server can accept the date in string format if it is in yyyy-mm-dd
private void btnInsert_Click(object sender, EventArgs e)
{
empCode = txtCode.Text;
empName = txtName.Text;
empCell = txtCell.Text;
empAddress = txtAddress.Text;
try
{
using (cmd = new SqlCommand(" empInsert ", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#empcode", SqlDbType.VarChar).Value = empCode;
cmd.Parameters.Add("#empname", SqlDbType.VarChar).Value = empName;
cmd.Parameters.Add("#empcell", SqlDbType.VarChar).Value = empCell;
cmd.Parameters.Add("#empaddress", SqlDbType.VarChar).Value = empAddress;
conn.Open();
cmd.ExecuteNonQuery();
}
MessageBox.Show("succesfully inserted", "Congrates");
}
catch (Exception ex)
{
MessageBox.Show("can't Insert there is error :" + ex, "Error");
}
finally
{
conn.Close();
}
}
Here is Stored procedure on SQL DB side.
use GDK
GO
create PROCEDURE dbo.empInsert
#id as VARCHAR(10,
#name as VARCHAR(10),
#cell as VARCHAR(10),
#address as VARCHAR(20)
AS
BEGIN
INSERT INTO EmployeeRecord(empcode,empname,empcell,empaddress) VALUES( #id, #name, #cell, #address)
END
I am unable to INSERT in DB.
Kindly help in this regard
You have parameter name #id in stored procedure but you are passing #empcode
Change
cmd.Parameters.Add("#empcode", SqlDbType.VarChar).Value = empCode;
To
cmd.Parameters.Add("#id", SqlDbType.VarChar).Value = empCode;
The problem is that your parameter names do not match.
From MSDN:
When using parameters with a SqlCommand to execute a SQL Server stored
procedure, the names of the parameters added to the Parameters
collection must match the names of the parameter markers in the stored
procedure.
So this is what you need:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#id", empCode);
cmd.Parameters.AddWithValue("#name", empName);
cmd.Parameters.AddWithValue("#cell", empCell);
cmd.Parameters.AddWithValue("#address", empAddress);
I have a stored procedure that returns a variable #result set to 1 or 0 (datatype bit). I am accessing it in my C# with the following code. Its throwing an error saying too many parameters.
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = "Registration";
Cmd.Parameters.AddWithValue("#Name", txtName.Text);
Cmd.Parameters.AddWithValue("#Email", txtEmailAddress.Text);
Cmd.Parameters.AddWithValue("#Password", txtPassword.Text);
Cmd.Parameters.AddWithValue("#CountryCode", ddlCountryCode.Text);
Cmd.Parameters.AddWithValue("#Mobile", txtMobileNumber.Text);
//Cmd.Parameters.Add("#Result", DbType.Boolean);
SqlParameter sqlParam = new SqlParameter("#Result", DbType.Boolean);
//sqlParam.ParameterName = "#Result";
//sqlParam.DbType = DbType.Boolean;
sqlParam.Direction = ParameterDirection.Output;
Cmd.Parameters.Add(sqlParam);
Cmd.ExecuteNonQuery();
con.Close();
Response.Write(Cmd.Parameters["#Result"].Value);
}
the stored procedure: (this I think is fine...) And please correct my CS code...
ALTER PROCEDURE [dbo].[usp_CheckEmailMobile](
#Name VARCHAR(50),
#Email NVARCHAR(50),
#Password NVARCHAR(50),
#CountryCode INT,
#Mobile VARCHAR(50),
#Result BIT OUTPUT)
AS
BEGIN
IF EXISTS (SELECT COUNT (*) FROM AUser WHERE [Email] = #Email AND [Mobile] = #Mobile)
Begin
Set #Result=0; --Email &/or Mobile does not exist in database
End
ELSE
Begin
--Insert the record & register the user
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (#Name, #Email, #Password, #CountryCode, #Mobile)
Set #Result=1;
End
END
you can try this code :
bool result=false;
SqlCommand scCommand = new SqlCommand("usp_CheckEmailMobile", sqlCon);
scCommand.CommandType = CommandType.StoredProcedure;
scCommand.Parameters.Add("#Name", SqlDbType.VarChar, 50).Value = txtName.Text;
scCommand.Parameters.Add("#Email", SqlDbType.NVarChar, 50).Value = txtEmailAddress.Text;
scCommand.Parameters.Add("#Password ", SqlDbType.NVarChar, 50).Value = txtPassword.Text;
scCommand.Parameters.Add("#CountryCode", SqlDbType.VarChar.50).Value =ddlCountryCode.SelectedText;
scCommand.Parameters.Add("#Mobile", SqlDbType.NVarChar, 50).Value = txtMobileNumber.Text;
scCommand.Parameters.Add("#Result ", SqlDbType.Bit).Direction = ParameterDirection.Output;
try
{
if (scCommand.Connection.State == ConnectionState.Closed)
{
scCommand.Connection.Open();
}
scCommand.ExecuteNonQuery();
result = Convert.ToBoolean(scCommand.Parameters["#Result"].Value);
}
catch (Exception)
{
}
finally
{
scCommand.Connection.Close();
Response.Write(result);
}
Why do you set:
Cmd.CommandText = "Registration";
this will replace your stored procedure name, so it won't call the stored procedure you indicated in:
SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
It can be useful to use a SQL profiler to debug that the SQL going "over the wire" is as expected.
I got the error:
Procedure or function usp_User_Info3 has too many arguments specified
When I run the program. I don't know the error in SP or in C# code. I have to display the Vendor_ID after the user clicks the submit button. Where the thing going wrong here ?
Table structure :
CREATE TABLE User_Info3
(
SNo int Identity (2000,1) ,
Vendor_ID AS 'VEN' + CAST(SNo as varchar(16)) PERSISTED PRIMARY KEY,
UserName VARCHAR(16) NOT NULL,
User_Password VARCHAR(12) NOT NULL,
User_ConPassword VARCHAR(12) NOT NULL,
User_FirstName VARCHAR(25) NOT NULL,
User_LastName VARCHAR(25) SPARSE NULL,
User_Title VARCHAR(35) NOT NULL,
User_EMail VARCHAR(35) NOT NULL,
User_PhoneNo VARCHAR(14) NOT NULL,
User_MobileNo VARCHAR(14)NOT NULL,
User_FaxNo VARCHAR(14)NOT NULL,
UserReg_Date DATE DEFAULT GETDATE()
)
Stored Procedure :
ALTER PROCEDURE [dbo].[usp_User_Info3]
#SNo INT OUTPUT,
#Vendor_ID VARCHAR(10) OUTPUT,
#UserName VARCHAR(30),
#User_Password VARCHAR(12),
#User_ConPassword VARCHAR(12),
#User_FirstName VARCHAR(25),
#User_LastName VARCHAR(25),
#User_Title VARCHAR(35),
#User_OtherEmail VARCHAR(30),
#User_PhoneNo VARCHAR(14),
#User_MobileNo VARCHAR(14),
#User_FaxNo VARCHAR(14)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,
User_LastName,User_Title,User_OtherEmail,User_PhoneNo,User_MobileNo,User_FaxNo)
VALUES (#UserName,#User_Password,#User_ConPassword,#User_FirstName,#User_LastName,
#User_Title,#User_OtherEmail,#User_PhoneNo,#User_MobileNo,#User_FaxNo)
SET #SNo = Scope_Identity()
SELECT Vendor_ID From User_Info3 WHERE SNo = #SNo
END
C# Code :
protected void BtnUserNext_Click(object sender, EventArgs e)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_User_Info3";
System.Data.SqlClient.SqlParameter SNo=cmd.Parameters.Add("#SNo",System.Data.SqlDbType.Int);
System.Data.SqlClient.SqlParameter Vendor_ID=cmd.Parameters.Add("#Vendor_ID",
System.Data.SqlDbType.VarChar,10);
cmd.Parameters.Add("#UserName", SqlDbType.VarChar).Value = txtUserName.Text;
cmd.Parameters.Add("#User_Password", SqlDbType.VarChar).Value = txtRegPassword.Text;
cmd.Parameters.Add("#User_ConPassword", SqlDbType.VarChar).Value = txtRegConPassword.Text;
cmd.Parameters.Add("#User_FirstName", SqlDbType.VarChar).Value = txtRegFName.Text;
cmd.Parameters.Add("#User_LastName", SqlDbType.VarChar).Value = txtRegLName.Text;
cmd.Parameters.Add("#User_Title", SqlDbType.VarChar).Value = txtRegTitle.Text;
cmd.Parameters.Add("#User_OtherEmail", SqlDbType.VarChar).Value = txtOtherEmail.Text;
cmd.Parameters.Add("#User_PhoneNo", SqlDbType.VarChar).Value =txtRegTelephone.Text;
cmd.Parameters.Add("#User_MobileNo", SqlDbType.VarChar).Value =txtRegMobile.Text;
cmd.Parameters.Add("#User_FaxNo", SqlDbType.VarChar).Value =txtRegFax.Text;
cmd.Connection = SqlCon;
try
{
Vendor_ID.Direction = System.Data.ParameterDirection.Output;
SqlCon.Open();
cmd.ExecuteNonQuery();
string VendorID = cmd.ExecuteScalar() as string;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode(" + VendorID + ");
SqlCon.Close();
}
}
You're not setting the direction of the #SNo parameter
You're calling the command twice - Just call it with ExecuteScalar if you want the return value.
You're not setting the value of your #Vendor_ID output parameter in the stored procedure.
If I had to guess, I would wager that cmd is being re-used and has parameters from a previous call left in it. One option would be to call cmd.Parameters.Clear(), but frankly I see little point re-using this SqlCommand instance - it would be better to use a new command each time:
using(var cmd = SqlCon.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_User_Info3";
// TODO: add parameters
// TODO: call one of the Execute* methods
}
User_OtherEmail column doesnt exist in the table
Correct Stored Procedure
Create PROCEDURE [dbo].[usp_User_Info3]
#SNo INT OUTPUT,
#Vendor_ID VARCHAR(10) OUTPUT,
#UserName VARCHAR(30),
#User_Password VARCHAR(12),
#User_ConPassword VARCHAR(12),
#User_FirstName VARCHAR(25),
#User_LastName VARCHAR(25),
#User_Title VARCHAR(35),
#User_Email VARCHAR(30),
#User_PhoneNo VARCHAR(14),
#User_MobileNo VARCHAR(14),
#User_FaxNo VARCHAR(14)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,
User_LastName,User_Title,User_Email,User_PhoneNo,User_MobileNo,User_FaxNo)
VALUES (#UserName,#User_Password,#User_ConPassword,#User_FirstName,#User_LastName,
#User_Title,#User_Email,#User_PhoneNo,#User_MobileNo,#User_FaxNo)
SET #SNo = Scope_Identity()
SELECT Vendor_ID From User_Info3 WHERE SNo = #SNo
END
and the c# code to be modified accordingly .
After all these answers from great minds I can't believe you are still stuck. Here are my suggestions
In your PROC
'maintain this in your parameter
#Vendor_ID VARCHAR(10) OUTPUT,
Update to Select VendorID into #VendorID. See the second line
SET #SNo = Scope_Identity()
SELECT #Vendor_ID=VendorID From User_Info3 WHERE SNo = #SNo
In C#, do not call ExecuteScalar use ExecuteNonQuery
string newVendorID = "";
try
{
Vendor_ID.Direction = System.Data.ParameterDirection.Output;
SqlCon.Open();
cmd.ExecuteNonQuery();
if(Vendor_ID.Value != null)
newVendorID = Vendor_ID.Value.ToString();
}
You could rename the Vendor_ID parameter to something more meaningful, like VendorIDParam. Hope this helps?
Thanks for all your response.After i modify many times the C# code and SP according to your ideas, finally i got the answer for my question. I removed 'Vendor_ID' from my SP and add few codes in my code as shown below.
Stored procedure :
ALTER PROCEDURE [dbo].[usp_User_Info3]
#SNo INT OUTPUT,
#UserName VARCHAR(30),
#User_Password VARCHAR(12),
#User_ConPassword VARCHAR(12),
#User_FirstName VARCHAR(25),
#User_LastName VARCHAR(25),
#User_Title VARCHAR(35),
#User_OtherEmail VARCHAR(30),
#User_PhoneNo VARCHAR(14),
#User_MobileNo VARCHAR(14),
#User_FaxNo VARCHAR(14)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,User_Title,User_OtherEmail,User_PhoneNo,User_MobileNo,User_FaxNo)
VALUES (#UserName,#User_Password,#User_ConPassword,#User_FirstName,#User_LastName,#User_Title,#User_OtherEmail,#User_PhoneNo,#User_MobileNo,#User_FaxNo)
SET #SNo = Scope_Identity()
END
C# Code :
protected void BtnUserNext_Click(object sender, EventArgs e)
{
SqlCon.Open();
SqlCommand cmd2 = new SqlCommand("usp_User_Info3", SqlCon);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "usp_User_Info3";
System.Data.SqlClient.SqlParameter SNo=cmd2.Parameters.Add("#SNo", System.Data.SqlDbType.Int);
cmd2.Parameters.Add("#UserName", SqlDbType.VarChar).Value = txtUserName.Text.Trim();
cmd2.Parameters.Add("#User_Password", SqlDbType.VarChar).Value = txtRegPassword.Text.Trim();
cmd2.Parameters.Add("#User_ConPassword", SqlDbType.VarChar).Value = txtRegConPassword.Text;
cmd2.Parameters.Add("#User_FirstName", SqlDbType.VarChar).Value = txtRegFName.Text.Trim();
cmd2.Parameters.Add("#User_LastName", SqlDbType.VarChar).Value = txtRegLName.Text.Trim();
cmd2.Parameters.Add("#User_Title", SqlDbType.VarChar).Value = txtRegTitle.Text.Trim();
cmd2.Parameters.Add("#User_OtherEmail", SqlDbType.VarChar).Value = txtOtherEmail.Text.Trim();
cmd2.Parameters.Add("#User_PhoneNo", SqlDbType.VarChar).Value = txtRegCode1.Text;
cmd2.Parameters.Add("#User_MobileNo", SqlDbType.VarChar).Value = txtRegCode2.Text;
cmd2.Parameters.Add("#User_FaxNo", SqlDbType.VarChar).Value = txtRegCode3.Text;
cmd2.Connection = SqlCon;
try
{
SNo.Direction = System.Data.ParameterDirection.Output;
cmd2.ExecuteScalar();
string VendorID = "VEN" + cmd2.Parameters["#SNo"].Value.ToString();
}
finally
{
string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode(" + VendorID + ");
ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Login created successfully');window.location.href = '" + url + "';", true);
SqlCon.Close();
}
}