This question already has an answer here:
Column <column name> does not belong to table
(1 answer)
Closed 4 years ago.
The thing is in my view employee page I have a option called edit where it goes to new page to edit the details of employee here.I am using this line to get the data from sql server to text box and it's showing this error, I have cross checked the column name hell lot of times I don't know where the error is. I have been using a stored procedure to get value from the database
I have all the process for this none helped me.
txtrcountry.Text = dtst.Tables[0].Rows[0]["RCountry"].ToString();
This is my stored procedure which is used to insert edited data to database
USE [PMS v1.0]
GO
/****** Object: StoredProcedure [dbo].[editemp] Script Date: 15-02-2019 09:54:19 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[editemp](#empid int )
as
begin
select Employeeid,firstname,lastname,Gender,convert(varchar(20),(convert(date,DateofBirthday,105))) as DateofBirthday,Mobilenumber,
Alternatenumber,Emailid,AlternateEmail,Fathername,
Mothername,AadhaarCardNo,PanCardNo,PassportNo,UserName,Password,RAddressLine1,RAddressLine2,RCity,RState,RZipCode,RCountry,
PAddressLine1,PAddressLine2,PCity,PState,PZipCode,PCountry,OAddressLine1,
OAddressLine2,OCity,OState,OZipCode,OCountry from newemployee with(nolock) where Employeeid=#empid
select sno,Employeeid,languagename,Expertise from employeelanguges with(nolock) where Employeeid=#empid
end
This is another stored procedure which is used to get the data from database
USE [PMS v1.0]
GO
/****** Object: StoredProcedure [dbo].[anotherpageupdateemp] ScriptDate: 15-02-2019 09:57:36 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[anotherpageupdateemp]
(
#Employeeid int,#firstname nvarchar(20),#lastname nvarchar(40),#Gender nvarchar(10),#DateOfBirth nvarchar(20),
#Fathername nvarchar(20),#Mothername nvarchar(20),
#Emailid nvarchar(20),#AlternateEmail nvarchar(20),#AadhaarCardNo nvarchar(20),
#PanCardNo nvarchar(20),#PassportNo nvarchar(20),#UserName nvarchar(20),
#Password nvarchar(20),#Mobilenumber nvarchar(20),#Alternatenumber nvarchar(20),#RAddressLine1 nvarchar(40),
#RAddressLine2 nvarchar(40),#RCity nvarchar(30),#RState nvarchar(30),#RZipCode nvarchar(20),#RCountry nvarchar(30),
#PAddressLine1 nvarchar(30),#PAddressLine2 nvarchar(30),
#PCity nvarchar(20),#PState nvarchar(20),#PZipCode nvarchar(20),#PCountry nvarchar(30),#OAddressLine1 nvarchar(30),#OAddressLine2 nvarchar(30),#OCity nvarchar(20),
#OState nvarchar(20),#OZipCode nvarchar(20),#OCountry nvarchar(30)
)
as
begin
update newemployee set firstname=#firstname,lastname=#lastname,Gender=#Gender,DateofBirthday=#DateOfBirth,Fathername=#Fathername,Mothername=#Mothername,
Emailid=#Emailid,AlternateEmail=#AlternateEmail,AadhaarCardNo=#AadhaarCardNo,PanCardNo=#PanCardNo,PassportNo=#PassportNo,UserName=#UserName,
Password=#Password,Mobilenumber=#Mobilenumber,Alternatenumber=#Alternatenumber,RAddressLine1=#RAddressLine1,
RAddressLine2=#RAddressLine2,RCity=#RCity,RState=#RState,RZipCode=#RZipCode,RCountry=#RCountry,PAddressLine1=#PAddressLine1,PAddressLine2=#PAddressLine2,
PCity=#PCity,PState=#PState,PZipCode=#PZipCode,PCountry=#PCountry,OAddressLine1=#OAddressLine1,OAddressLine2=#OAddressLine2,OCity=#OCity,OState=#OState,OZipCode=#OZipCode,OCountry=#OCountry
where Employeeid=#Employeeid
end
txtrcountry is the textbox id and RCountry is the column name I should get the details of the country in the textbox.
Your number of parameters in C# side isn't same as the stored procedure. Your parameters should correspond like this:
#Employeeid
#firstname
#lastname
#Gender
#DateOfBirth
#Fathername
#Mothername
#Emailid
#AlternateEmail
#AadhaarCardNo
#PanCardNo
#PassportNo
#UserName
#Password
#Mobilenumber
#Alternatenumber
#RAddressLine1
#RAddressLine2
#RCity
#RState
#RZipCode
#RCountry
#PAddressLine1
#PAddressLine2
#PCity
#PState
#PZipCode
#PCountry
#OAddressLine1
#OAddressLine2
#OCity
#OState
#OZipCode
#OCountry
yes I have solved the whole error Thanks #GaganDeep and #Gauravsa for helping me this error is solved
I have passed an extra argument in C# code
cmd.Parameters.AddWithValue("#Role", empenty.Role);
I removed this line which solved my Bug
I'm using Microsoft SQL Server 2014 and I have a stored procedure in my database called spLogin that returns a boolean to indicate whether or not login was successful.
This is the stored procedure:
USE [MyDataBase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spLogin]
#loginName NVARCHAR(50),
#password NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
DECLARE #bit BIT
IF NOT EXISTS (SELECT TOP 1 User.UserID FROM User WHERE User.LoginName=#loginName)
OR RTRIM(LTRIM(#loginName)) IS NULL OR RTRIM(LTRIM(#loginName)) IS NULL
SET #bit=0
ELSE
BEGIN
DECLARE #userID INT
SET #userID= (SELECT User.UserID FROM User WHERE User.LoginName=#loginName
AND User.Password=HASHBYTES('SHA2_512', #password+CAST(User.Salt AS NVARCHAR(36))))
IF(#userID IS NULL)
SET #bit = 0
ELSE
SET #bit = 1
END
RETURN #bit
END
When I execute this stored procedure on Microsoft SQL Server 2014 Management Studio, it returns 1 if I pass the correct arguments. For example:
USE [MyDataBase]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[spLogin]
#loginName= N'adminCharles',
#password= N'admin123'
SELECT 'Return Value' = #return_value
GO
In this case, it returns the value I expected, which is 1.
But, when I use Entity Framework on a WPF project, it returns -1 even though I'm passing the correct arguments. For instance:
DataBaseEntities context = new DataBaseEntities();
var login = context.spLogin("adminCharles","admin123");
//output: -1
In that case, it outputs -1 and I just simply don't know why.
I would really appreciate some help here. Thanks in advance.
And by the way, this is just a simple project for learnign Entity Framework.
EDIT
I changed #bit BIT for INT and at the end of the stored procedure, I wrote this:
SET #bit=0
RETURN #bit
I updated my project and I'm still getting -1. Could it be a permissions issue?
I'm trying to create a stored proc for the first time, but I just can't get it right. So I have a proc which is returning a count from the db, based on the parameters being fed to it. I'm not sure what I'm doing wrong, if the SQL is wrong or if I'm mapping the function incorrectly. Here's my SQL:
USE [AuditTracker_Codegen]
GO
/****** Object: StoredProcedure [dbo].[GetAllFindingsCount] Script Date: 05/12/2016 08:43:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetAllFindingsCount](#ReportId int, #Priority int, #IsDeleted bit)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COUNT(*)
from [dbo].[App_Finding] finding
WHERE finding.ReportId = #ReportId and finding.Priority = #Priority and finding.IsDeleted = #IsDeleted
END
First off, does this SQL query look right? I've executed it in SSMS and it seems to return the correct result.
I then go to VS, update model from database, add the stored procedure.
Then I go to Function Imports in Model Browser, right-click, Add Function Import. Select the stored proc from the dropdown list, click "Get Column Information", then press "Create New Complex Type". Give the thing a name, then press OK. As far as I know, that should be it, right? Or am I wrong?
In my code I then try
var context = new AuditTrackerDatabaseContext();
var findingsOne = context.GetAllFindingsCount_Result(report.ReportId,
(int) PriorityStatus.One, false).ToString();
And that's where I get the error above. When I check mapping details on the edmx for this proc, it says
Maps to GetAllFindingsCount Column
Column1 : Int32 <- Column1
Is anyone able to advise me on what I'm doing wrong here, why it's not working?
You probably need to give the count value a name, like this:
SELECT COUNT(*) AS RESULT_OF_COUNT
from ...
This is my stored procedure.Someone says using TVP i can get ouput i want. But i am new to sql. So please someone help me, how to pass multiple disposeid's and multiple receiveid's which shown below.Right now i need output only using that I can able to understand what is tvp so someone please edit my SP to pass multiple values in it.
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[up_Get_Customers] Script Date: 09/23/2015 19:10:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[up_Get_Customers]
(
#DisposeId int
)
AS
BEGIN
SELECT C1.DisposeDate,C1.DisposeID,C1.ReceiveDate,C1.ReceiveID
FROM Customers C1
LEFT JOIN Customers C2 ON C1.DisposeID=C1.ReceiveID
WHERE #DisposeId IS NULL OR (C1.DisposeID in (#DisposeId,','))
END
GO
Well, this is something new I've learned today, with a tiny amount of Googling I came up with this
https://msdn.microsoft.com/en-us/library/bb510489%28SQL.100%29.aspx
TVP being Table Valued Parameters.
So based on the information in the link:
CREATE TYPE DisposeIdsTable AS TABLE (
DisposeId VARCHAR(50));
GO
and then
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[up_Get_Customers] Script Date: 09/23/2015 19:10:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[up_Get_Customers]
(
#DisposeIds DisposeIdsTable READONLY
)
AS
BEGIN
SELECT C1.DisposeDate,C1.DisposeID,C1.ReceiveDate,C1.ReceiveID
FROM Customers C1
LEFT JOIN Customers C2 ON C1.DisposeID=C1.ReceiveID
WHERE (SELECT COUNT(*) FROM #DisposeIds) = 0
OR
(C1.DisposeID IN (
SELECT DisposeID FROM #DisposeIds
UNION ALL
SELECT ',')))
END
GO
N.B. The code posted above is not tested at all, so may contain mistakes. Also I've made an assumption about the datatype of DisposeId, so that's unlikely to be correct and would need amending to suit your datatype. Also, I've attempted to preserve the existing logic of the query, whatever that may be.
When executing the stored procedure you will instead need to declare a variable of type DisposeIdsTable rather than the text datatype that you're currently using. That table variable will then need to be populated with your IDs.
This is my procedure :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uspChangeMemberRole]
#TeamID INT,
#MemberID INT,
#MemberRole TINYINT
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[TeamMember]
SET MemberRole = #MemberRole
WHERE TeamID = #TeamID
AND MemberID = #MemberID;
RETURN #MemberRole;
END
When I execute this procedure I want to return "MemberRole Successfully Changed".
Please give me a solution how to return
Use Row Count to check if there are affected rows (identified if update is successful) then select a string.
RowCount - Returns the number of rows affected by the last statement.
IF ##ROWCOUNT > 0
BEGIN
SELECT 'Member Role Successfully Chnaged';
END
And I think this statement is not necessary since this is an input parameter, you can get outside the proc.
return #MemberRole;
Change your return to select, and replace the datatype of your output parameter with varchar.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uspChangeMemberRole]
#TeamID INT,
#MemberID INT,
#MemberRole VARCHAR(100) OUTPUT
AS BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[TeamMember] SET MemberRole=#MemberRole WHERE TeamID=#TeamID AND MemberID=#MemberID;
SET #MemberRole = 'MemberRole Successfully Changed.'
SELECT #MemberRole AS 'RETURN';
I am adding some of the concept that will help you to understand how Procedure's will return values.
If you want to return something from SQL Server to Frontend, Just use "select" statement.
You have to use proper command from frontend like you want to return scalar value, or query.