I am inserting some values into SQL Server table using C#. Actually I want to insert values and return the 'ID' of last inserted record. I use the following code:
string name = txt_name.Text.Trim();
string gender = Gender.Text.Trim();
string citizen = Citizen.Text.Trim();
int IdNo = Convert.ToInt32(idnumber.Text);
BUSSINESSACCESS.SENDMESSAGES send = new BUSSINESSACCESS.SENDMESSAGES();
DataSet dt = send.insertvoucher(name, gender, citizen, IdNo);
Business layer
public DataSet insertvoucher(string name, string gender, string citizen, int IdNo)
{
SqlParameter[] par = {
new SqlParameter("@Name", name),
new SqlParameter("@Citizen", citizen),
new SqlParameter("@Emiratesidno", IdNo),
new SqlParameter("@gender", gender),
};
var rowCount = SHJCSQLHELPER.ExecuteScalar(CONNECTIONSTRING, CommandType.StoredProcedure, "[INSERTVOUCHER]", par);
return rowCount;
// return SHJCSQLHELPER.ExecuteDataset(CONNECTIONSTRING, CommandType.StoredProcedure, "[INSERTVOUCHER]", par);
//string mn = "";
}
Stored procedure
-- Insert statements for procedure here
Insert into Usertable(Name, Citizen, Emiratesidno, gender)
values (@Name, @Citizen, @Emiratesidno, @gender)
SELECT scope_identity() AS int
I am getting the value in the variable rowcount but I cannot return the variable rowcount because it is saying
Cannot implicitly convert type 'object' to 'system.data.dataset'
InsertVoucheris supposed to return aDataSet.insertvoucherisDataSetbut you returningobject. I think you need change return type tointand then castrowCountto integerreturn (int)rowCount;rowCountis misleading, since you expectingid