TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Salem Al
NA
12
741
Insert to database MVP C#
Dec 20 2020 12:43 PM
Hi ....
I am trying to insert data to database table but i need it check before insert if the inserted data is Exists in database table iam getting this error :
CS0120 An object reference is required for the non-static field, method, or property 'ManageVisitsTypesService.InsertVisitsTypes(int, string, bool, string, int)'
I sure this Method is not Static.
First i have Stored Procedure:
Create
procedure
[dbo].[Save_Visits_Types]
@VisitTypeID
int
output
,
@VisitTypeName nvarchar (
Max
),
@VisitTypeStatus
bit
,
@VisitTypeNotes nvarchar (
Max
),
@CreatedBy
int
As
BEGIN
SET
NOCOUNT
ON
;
If Exists(
SELECT
1
FROM
dbo.VisitsTypesTbl
WHERE
VisitTypeID = @VisitTypeID)
BEGIN
Return
-3;
END
;
If Exists(
SELECT
1
FROM
dbo.VisitsTypesTbl
WHERE
VisitTypeName = @VisitTypeName)
BEGIN
Return
-4;
END
;
INSERT
INTO
dbo.VisitsTypesTbl (VisitTypeID, VisitTypeName, VisitTypeStatus, VisitTypeNotes, CreatedDate, CreatedDeviceName)
VALUES
(@VisitTypeID, @VisitTypeName, @VisitTypeStatus, @VisitTypeNotes, getdate(), HOST_NAME());
Return
-2;
END
Then i have Data Access Layer Which i named DBHelper :
//Method To Insert , Update And Delete From Database
public
static
bool
ExcuteData(
string
SpName, Action method)
{
using
(SqlConnection connection = GetConnectionString())
{
try
{
command =
new
SqlCommand(SpName, connection);
command.CommandType = CommandType.StoredProcedure;
method.Invoke();
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return
true
;
}
catch
(Exception ex)
{
connection.Close();
Console.WriteLine(ex.Message);
return
false
;
}
finally
{
connection.Close();
}
}
}
Then i have Created Model Class Which contain :
namespace
Team_Manager.Models
{
public
class
ManageVisitsTypesModel
{
public
int
VisitTypeID {
get
;
set
; }
public
string
VisitTypeName {
get
;
set
; }
public
bool
VisitTypeStatus {
get
;
set
; }
public
string
VisitTypeNotes {
get
;
set
; }
public
int
CreatedBy {
get
;
set
; }
}
}
Then i have created the Service Class which contain the insert Method :
//This Method For Insert Paramerters For Visits Types To Database
public
SaveResult InsertVisitsTypesParamerter(
int
VisitTypeID,
string
VisitTypeName,
bool
VisitTypeStatus,
string
VisitTypeNotes,
int
CreatedBy, SqlCommand command)
{
var pReturnValue =
new
SqlParameter(
"@ReturnValue"
, SqlDbType.Int) { Direction = ParameterDirection.ReturnValue };
command.Parameters.Add(
"@VisitTypeID"
, SqlDbType.Int).Value = VisitTypeID;
command.Parameters.Add(
"@VisitTypeName"
, SqlDbType.NVarChar).Value = VisitTypeName;
command.Parameters.Add(
"@VisitTypeStatus"
, SqlDbType.Bit).Value = VisitTypeStatus;
command.Parameters.Add(
"@VisitTypeNotes"
, SqlDbType.NVarChar).Value = VisitTypeNotes;
command.Parameters.Add(
"@CreatedBy"
, SqlDbType.Int).Value = CreatedBy;
switch
((
int
)pReturnValue.Value)
{
case
-2:
return
SaveResult.Ok;
case
-3:
return
SaveResult.DuplicateId;
case
-4:
return
SaveResult.DuplicateName;
default
:
return
SaveResult.UnknownError;
}
}
//This Method For Insert Visits Types To Database
public
bool
InsertVisitsTypes(
int
VisitTypeID,
string
VisitTypeName,
bool
VisitTypeStatus,
string
VisitTypeNotes,
int
CreatedBy)
{
return
ExcuteData(
"Save_Visits_Types"
, () => InsertVisitsTypesParamerter(VisitTypeID, VisitTypeName, VisitTypeStatus, VisitTypeNotes, CreatedBy, command));
}
And Presenter Class :
class
ManageVisitsTypesPresenter
{
IManageVisitsTypes iManageVisitsTypes;
ManageVisitsTypesModel ManageVisitsTypesModel =
new
ManageVisitsTypesModel();
public
ManageVisitsTypesPresenter(IManageVisitsTypes view)
{
iManageVisitsTypes = view;
}
// Connect Between Model And Interface
private
void
ConnectManageVisitsTypesModelAndInterface()
{
ManageVisitsTypesModel.VisitTypeID = iManageVisitsTypes.VisitTypeID;
ManageVisitsTypesModel.VisitTypeName = iManageVisitsTypes.VisitTypeName;
ManageVisitsTypesModel.VisitTypeStatus = iManageVisitsTypes.VisitTypeStatus;
ManageVisitsTypesModel.VisitTypeNotes = iManageVisitsTypes.VisitTypeNotes;
ManageVisitsTypesModel.CreatedBy = iManageVisitsTypes.CreatedBy;
}
// Insert Visits Types
public
void
VisitsTypesInsert()
{
if
(iManageVisitsTypes.VisitTypeName ==
""
)
{
iManageVisitsTypes.TheError.SetError(iManageVisitsTypes.TheStatus,
"Please Type Visit Type"
);
return
;
}
SaveResult saveResult = ManageVisitsTypesService.InsertVisitsTypes(ManageVisitsTypesModel.VisitTypeID, ManageVisitsTypesModel.VisitTypeName, ManageVisitsTypesModel.VisitTypeStatus, ManageVisitsTypesModel.VisitTypeNotes, ManageVisitsTypesModel.CreatedBy);
if
(saveResult == SaveResult.DuplicateId)
{
MessageBox.Show(
"This ID Type Allready Exists"
);
}
else
if
(saveResult == SaveResult.DuplicateName)
{
MessageBox.Show(
"This Type Name Allready Exists"
);
}
else
{
ConnectManageVisitsTypesModelAndInterface();
GetActiveVisitsTypes();
AutoNumber();
XtraMessageBox.Show(DevExpress.LookAndFeel.UserLookAndFeel.Default,
"Visit Type Saved Successflly ..."
,
"Add Visit Type"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Reply
Answers (
4
)
Need to make some design decision based on Kafka and Cassandra
Strategy for integrating multiple APIs for multi-tenant application