'Column 'RCountry' does not belong to table Table.' [duplicate] - c#

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

Related

Adding id value into ecaxtly id column

I'm using SQL Managment Studio to connect my database and I'm trying to add some rows into it in Windows App using Stored Procedures.
Whenever I add a row the id value adds into a new column1 as it shown at the screenshot. How can i fix it?
dataGridView1.Columns["id"].ReadOnly = true;
Stored Procedure:
USE [schedule]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_insertProduct]
#date date,
#less_n int,
#teacher nchar,
#room int,
#gr int,
#subject nchar
AS
INSERT INTO dbo.main(date, less_n, teacher, room, gr, subject) VALUES
(#date, #less_n, #teacher, #room, #gr, #subject)
SELECT SCOPE_IDENTITY()
Screenshoot

C# MVC EF error - data reader incompatible member of the type does not have a corresponding column in the data reader with the same name

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 ...

How to use the output parameter in Linq?

I want to use the #ResultGroup as a output parameter in C#.
Please help me do this.
This is my stored procedure:
USE [DBAzmoon]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SearchInGroup]
#GroupName nvarchar(50),
#ResultGroup int output
AS
if(Exists(select* From TblGroup where GroupName=#GroupName))
set #ResultGroup=1
else
Begin
set #ResultGroup=0
Insert Into TblGroup(GroupName) Values (#GroupName)
End
You can use like this :
int ResultGroup;
context.SearchInGroup(GroupName ,ref ResultGroup)
Try to follow this post:
http://weblogs.asp.net/scottgu/linq-to-sql-part-6-retrieving-data-using-stored-procedures

How to pass multiple values in a parameter with stored procedure

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.

Problem in stored procedure

create procedure InsertQuestionEntry
#round_name varchar(40),
#question varchar(100),
#answer varchar(40),
#option1 varchar(20),
#option2 varchar(30),
#option3 varchar(30)
as
begin
insert into QuestionEntry(Question,Round_Name) values(#question,#round_name);
declare #quesion_id int
exec #quesion_id= select Question_ID from QuestionEntry;
insert into Answer(Question_ID,Answer,Option1,Option2,Option3) values(#quesion_id,#answer,#option1,#option2,#option3);
end
Here I want to retrieve the Question_ID from table QuestionEntry and use that Question_ID to another table Answer
But this didn't work.
So how can I use above way?
please help me
Instead of
insert into QuestionEntry(Question,Round_Name) values(#question,#round_name);
declare #quesion_id int
exec #quesion_id= select Question_ID from QuestionEntry;
use the following:
DECLARE #quesion_id int
INSERT INTO QuestionEntry(Question,Round_Name) values(#question,#round_name)
SET #quesion_id = SCOPE_IDENTITY()
You should not use "exec" there.
What exec does is:
Executes a command string or character
string within a Transact-SQL batch, or
one of the following modules: system
stored procedure, user-defined stored
procedure, scalar-valued user-defined
function, or extended stored
procedure.
You should use "set" or "select" instead of exec.
SET can only assign one variable at
a time, SELECT can make multiple
assignments at once. When assigning
from a query if there is no value
returned then SET will assign
NULL, where SELECT will not make
the assignment at all (so the variable
will not be changed from it's previous
value)
You can find more info about when to use SET or SELECT here: SET vs SELECT when assigning variables
Sample:
set #quesion_id = (select Question_ID from QuestionEntry)
select #quesion_id = (select Question_ID from QuestionEntry)
But that's also wrong way to get identity value from inserted record. If you have N users execute a same procedure at a same time it can happen that you will get wrong value (from last inserted record).
To do this properly you should use ##IDENTITY or even better SCOPE_IDENTITY(). More info: here.
After INSERT you can simply call:
SELECT #quesion_id = ##IDENTITY
--or
SELECT #quesion_id = SCOPE_IDENTITY()
Also, check your Question_ID is configured properly. It should be set to auto increment.
Sample:
Question_ID int IDENTITY(1,1)PRIMARY KEY CLUSTERED
The 1's following the IDENTITY keyword indicate the SEED number (value for first record in table) and increment property (0 or 1).
If your server's version is SQL Server 2005 or higher, you could also try something like this:
create procedure InsertQuestionEntry
#round_name varchar(40),
#question varchar(100),
#answer varchar(40),
#option1 varchar(20),
#option2 varchar(30),
#option3 varchar(30)
as
begin
insert into QuestionEntry(Question,Round_Name)
output inserted.Question_ID, #answer, #option1, #option2, #option3
into Answer (Question_ID, Answer, Option1, Option2, Option3)
values(#question,#round_name);
end

Categories