Reputation: 1
I am trying to Wcf Service in Console Application. Insert, update and delete operation is working by wcf service with console application but i am unable to retrieve data from database by using wcf service in Console Application. I have local class which have the required data members and i defined a method inside wcf service which expect a parameter called account number. In the console application user enter the account number and based on this account the console should retrieve all data from sql database but its only retrieve account number which I provide when I run Console Application . If anyone help me on I will be grateful.
Here is the code for local class..
[DataContract]
public class AccountBalanceRequest : Current_Account_Details
{
string account_number;
[DataMember]
public string Account_Number
{
get { return account_number; }
set { account_number = value; }
}
}
}
Here is ADO.NET CODE..
public bool AccountBalanceCheek(AccountBalanceRequest accountNumber)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM
Current_Account_Details WHERE Account_Number ='" +
accountNumber.Account_Number + "'", conn))
{
cmd.Parameters.AddWithValue("@Account_Number",
accountNumber.Account_Number);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
return true;
}
}
}
Here is Console Application Code..
public static void Balance()
{
MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccou
ntService");
MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest();
string AccountNumber;
Console.WriteLine("\nEnter your Account Number--------:");
AccountNumber = Console.ReadLine();
cs.Account_Number = AccountNumber;
MyService.AccountBalanceRequest cs1 =
currentAccount.AccountBalanceCheek(cs);//Error on this line.
Console.WriteLine("Your Account Number is :" +cs.Account_Number);
Console.WriteLine("Your Account Type :" + cs.Account_Balance);
Console.WriteLine("Your Account Account Fee :" + cs.Account_Fee);
Console.WriteLine("Your Account Balance:" + cs.Account_Balance);
Console.WriteLine("Your Account Over Draft Limit :" + cs.Over_Draft_Limit);
Console.Write("--------------------------");
Console.ReadLine();
}
Here is screen shot of out put when i run the application..
Upvotes: 0
Views: 501
Reputation: 1909
Your method AccountBalanceCheek
is not retrieving data from your DB because it was using ExecuteNonQuery
and it only returns bool
What you can do is:
//change return type from bool to AccountBalanceRequest
public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
//use top 1 since you are only getting one record.
//let us use string interpolation, if you are working below C#6
//replace it with your previous value
var cmd = new SqlCommand($@"SELECT TOP 1
*
FROM
Current_Account_Details
WHERE
Account_Number ='{accountNumber.Account_Number}'", conn));
cmd.CommandType = CommandType.Text;
//use ExecuteReader to execute sql select
//ExecuteNonQuery is for update, delete, and insert.
var reader = cmd.ExecuteReader();
//read the result of the execute command.
while(reader.Read())
{
//assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details
//assuming that your datatype are string... just do the conversion...
accountNumber.Account_Balance = reader["Account_Balance"].ToString();
accountNumber.Account_Fee = reader["Account_Fee"].ToString();
accountNumber.Account_Balance = reader["Account_Balance"].ToString();
accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
}
return accountNumber;
}
}
In your console app code you must use cs1
to output your values.
//previously returns bool, now it returns AccountBalanceRequest
MyService.AccountBalanceRequest cs1 =currentAccount.AccountBalanceCheek(cs);
//use cs1
Console.WriteLine("Your Account Number is :" +cs1.Account_Number);
Console.WriteLine("Your Account Type :" + cs1.Account_Balance);
Console.WriteLine("Your Account Account Fee :" + cs1.Account_Fee);
Console.WriteLine("Your Account Balance:" + cs1.Account_Balance);
Console.WriteLine("Your Account Over Draft Limit :" + cs1.Over_Draft_Limit);
Upvotes: 2