Calling a parameterized stored procedure using WCF and LINQ to SQL - c#

How can I call a parameterized stored procedure usp_Get_Products from my WCF service? I have tried calling it without any parameters it works fine but I don't know how to pass a textbox value as a parameter from my About.xaml.cs file.
Service.svc.cs file
public List<Product> GetProducts()
{
ProductDataClassDataContext db = new ProductDataClassDataContext();
var products = db.usp_Get_Products();
return products.ToList();
}
Views/About.xaml.cs file
private void OnSearchProductClick(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client webService = new ServiceReference1.Service1Client();
webService.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);
webService.GetProductsAsync();
txtblcName.Text = txtName.Text; //Send this as parameter to usp_Get_Products
}
public void webService_GetProductsCompleted(object sender, ServiceReference1.GetProductsCompletedEventArgs e)
{
ProductGrid.ItemsSource = e.Result; //Binding Datagrid
}
How can I send txtName.Text from my xaml.cs file as parameter when calling the stored procedure? I know I'm missing some concepts, I'm new to WCF plus I have never worked on silverlight..

Are you sure the stored procedure accepts parameters?
Also can you please post your ProductDataClassDataContext.
Ideally you should modify your service contract to accept the parameters,
in case you don't wanna do it
you can do a filter on the products list like
var products = await webService.GetProductsAsync();
var filteredProduct = products.Where(item=> item.Name == txtName.Text);

Related

SQLite with Dapper C# - Windows Form: Using ComboBox Lisl Properties to retrieve database data with a foreign key

I work with C# in Visual Studio 2019. My database is in SQLite using Dapper.
Here is what I am struggling with.
I have 2 tables in my database that are connected.
The parent, tbClient. And the child table, tbProject.
tbProject has a field to ClientId.
I use a ComboBox to WireUpp the Data from the database to my form. I have a form to Client, and a form for the Project, in this form I chose a CLient in a ComboBox, and save its ID in my tbProjet.
The idea is simple, but I am struggling because I am using an example that was made in Windows (WPF), and my application is in Windows Forms. I noticed that the Properties of the ComboBox are not the same, then I am having some trouble accessing the correct Project field when I want to open the Project of a specific Client.
Let´s show how it's done in the WPF app and in my WinForm app. I think is going to be more clear to get some help.
The codes of the Project Form are below: the first is the WPF app, and the second one is the WinForm where I was not able to make work yet.
WPF Application:
// WPF Application:
namespace MyApp.Controls
{
public ProjectControls()
{
InitializeComponent();
InitializeClientList();
WireUpDropDowns();
}
private void WireUpDropDowns()
{
clientDropDown.ItemsSource = clients;
clientDropDown.DisplayMemberPath = "Name";
clientDropDown.SelectedValuePath = "Id";
projectDropDown.ItemsSource = projects;
projectDropDown.DisplayMemberPath = "DisplayValue";
projectDropDown.SelectedValuePath = "Id";
}
private void InitializeClientList()
{
string sql = "select * from Client order by Name";
var clientList = SqliteDataAccess.LoadData<ClientModel>(sql, new Dictionary<string, object>());
clientList.ForEach(x => clients.Add(x));
}
private void clientDropDown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LoadProjectDropDown();
}
private void LoadProjectDropDown()
{
string sql = "select * from tbProject where ClientId = #ClientId";
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{ "#ClientId", clientDropDown.SelectedValue }
};
var records = SqliteDataAccess.LoadData<ProjectsModel>(sql, parameters);
projects.Clear();
records.ForEach(x => projects.Add(x));
}
}
Windows Form Application:
// Windows Forms Application:
namespace MyApp.Controls
{
public ProjectControls()
{
InitializeComponent();
InitializeClientList();
WireUpDropDowns();
}
private void WireUpDropDowns()
{
clientDropDown.DataSource = null;
clientDropDown.DataSource = clients;
clientDropDown.DisplayMember= "Name";
clientDropDown.ValueMember = "Id";
projectDropDown.DataSource = null;
projectDropDown.DataSource= projects;
projectDropDown.DisplayMember = "DisplayValue";
projectDropDown.ValueMember= "Id";
}
private void InitializeClientList()
{
string sql = "select * from Client order by Name";
var clientList = SqliteDataAccess.LoadData<ClientModel>(sql, new Dictionary<string, object>());
clientList.ForEach(x => clients.Add(x));
}
private void clientDropDown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LoadProjectDropDown();
}
private void LoadProjectDropDown()
{
string sql = "select * from tbProject where ClientId = #ClientId";
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{ "#ClientId", clientDropDown.SelectedValue }
// --> I think the problem is here, I am passing an object to the database where ClientId is an integer type. I tried to use SelectedIndex instead, but with this property, I do not get the correct Project from the table
};
var records = SqliteDataAccess.LoadData<ProjectsModel>(sql, parameters);
projects.Clear();
records.ForEach(x => projects.Add(x));
}
}
In the Windows Form Application I get this Error Message from my AccesDataBase Routine:
System.NotSupportedException: 'The member ClientId of type AppLibrary.Models.ClientModel cannot be used as a parameter value'
So I think the basic question here is Am I using the ComboBOx Properties correct? What I am missing?
Thank you in advance for any help received.
Verônica.
I found out about my mistake.
The property of the Combobox is correct.
I was passing the wrong parameter to the ClientDropDown.SelectedValue in other parts of the code that I haven´t shared here.
I was trying to select a specific client in the ClientDropDown through code but I was passing SelectedIndex to the SelectedValue.
I used the Breakpoints and was able to find the error, and now is working with this code that I share here in the question, it is correct.

c# methods not running in the sequence I wrote them

I got two standard projects in a solution. The UI and the Logic.
As usual, you need to take the inputs from the UI and do whatever you want with them in the back end part.
So in the UI class, I have this
private void btnAddItems_Click(object sender, RoutedEventArgs e)
{
item_name = lbl_item_name.Text;
item_quantity = lbl_item_quantity.Text;
store_ime = store_Name.Text;
logika.storeInDb(store_ime, item_name, item_quantity);
}
It just stores the input in variables and then sends them to this
public void storeInDb(string store_name, string item_name, string item_quantity)
{
using (MySqlConnection mySqlConn = new MySqlConnection(Logic.connStr))
{
dbInsert($"INSERT INTO soping(store_name, item_name, item_quantity, payment_type, date) VALUES('{store_name}', '{item_name}', '{item_quantity}', 'visa', 'danas')");
}
}
And this is the dbInsert method
public void dbInsert(string query)
{
using (MySqlConnection mySqlConn = new MySqlConnection(Logic.connStr))
{
try
{
mySqlConn.Open();
MySqlCommand cmd = new MySqlCommand(query, mySqlConn);
cmd.ExecuteNonQuery();
mySqlConn.Close();
}
catch (MySqlException e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}
}
It doesn't store anything. And when I use breakpoints, it seems like the button method runs after storeInDb, even though the variables in the query are perfectly fine. And I can't find anything wrong with the code that would make it behave weird like this.
This code have some issues:
1- You should use parameters instead of direct strings in your sql query;
2- You don't need a connection outside your dbInsert Method
However, this code should work. I guess the problem you are having is located elsewhere, not in the code you posted here. Something simpler, maybe connectionstring problem (saving in other place where you don't expect to) or bad uses of threads...Maybe hitting deadlocks, long processing or something like that (the only way i can think of having button click apparently happenning after the code it calls).

Trying to iterate through ListBox, Error : Specified cast is not valid

I have a method in another class i'm using to send data to a database. That method is here as well.
public Int32 AddOrder(clsStock NewItem)
{
//this function takes the data passed via NewItem
//and passes it to the parameters for the stored procedure
//
//create an instance of the data dictionary
clsDataDictionary DD = new clsDataDictionary();
//create an instance of the object class
Int32 ReturnValue;
//create an instance of the data conduit
clsDataConduit Items = new clsDataConduit();
//pass the data for this address
Items.AddParameter(DD.sproc_tblOrders_Add_AuthId, NewItem.AuthId);
Items.AddParameter(DD.sproc_tblOrders_Add_ItemId, NewItem.ItemId);
Items.AddParameter(DD.sproc_tblOrders_Add_DateOrdered, NewItem.DateOrdered);
Items.AddParameter(DD.sproc_tblOrders_Add_Cancel, NewItem.Cancel);
//execute the stored procedure
ReturnValue = Items.Execute(DD.sproc_tblOrders_Add);
//return the primary key value
return ReturnValue;
}
The method on my aspx page which i'm using to iterate through my listbox and execute that method for each item in the listbox is here as well.
protected void btnSubmit_Click1(object sender, EventArgs e)
{
//create an instance of the collection class
clsStockCollection Items = new clsStockCollection();
foreach(int id in lstAdded.Items)
{
TheItem.AuthId = 5;
TheItem.ItemId = Convert.ToInt32(lstAdded.Items[id].Value);
TheItem.Cancel = "false";
Items.AddOrder(TheItem);
}
Response.Redirect("Order.aspx");
}
When I run my website and hit the btnSubmit it's giving the following error :
"Specified cast is not valid" that is on the method on the aspx page (the 2nd pastebin file)
Any idea why this is?
It should be like this
foreach(ListItem item in lstAdded.Items)
{
TheItem = new clsStock();
TheItem.AuthId = 5;
TheItem.ItemId = Convert.ToInt32(item.Value);
TheItem.Cancel = "false";
Items.AddOrder(TheItem);
}
You are iterating the ListBox.Items through an int type field. ListBox.Items is a ListItemCollection, what you can do is use implicitly typed variable using var keyword, like:
foreach(var id in lstAdded.Items)
{
TheItem.AuthId = 5;
TheItem.ItemId = Convert.ToInt32(id.Text); //Change here
TheItem.Cancel = "false";
Items.AddOrder(TheItem);
}
Currently it appears you are considering it as an index in foreach loop, instead its a single item from the lstAdded

Delete from 2 SQL tables with SilverLight Linq-to-SQL

I got 2 tables in SQL that both have a field called Selection_ID. I want to delete all rows with Selection_ID = inpSelectionID using the Linq-to-SQL.
My tables:
My C# function:
void buttonDeleteSelectionList_Click(object sender, RoutedEventArgs e, Canvas canvasAdvancedSearchFieldResults, int inpSelectionID)
{
PositionServiceReference.PositionServiceClient service = new PositionServiceReference.PositionServiceClient();
service.DeleteSelectionCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(service_DeleteSelectionCompleted);
service.DeleteSelectionAsync(inpSelectionID);
}
My Service Code PositionService.svc.cs:
[OperationContract]
void DeleteSelection(int inpSelectionID)
{
PositionDataClassesDataContext context = new PositionDataClassesDataContext();
context.Lloyds_Selection.DeleteOnSubmit(inpSelectionID);
context.SubmitChanges();
context.Lloyds_Selection_Vessels.DeleteOnSubmit(inpSelectionID);
context.SubmitChanges();
}
DeleteOnSubmit requires the entity object as a parameter, so you can't delete item without selecting it from database first. There is DeleteAllOnSubmit method too, but it requires IEnumerable of entities as well. You can use it like that:
context.Lloyds_Selection.DeleteAllOnSubmit(context.Lloyds_Selection.Where(l => l.Selection_ID == inpSelectionID));
However, you can use DataContext.ExecuteCommand to execute raw SQL against your database:
string command = string.format("DELETE FROM Lloyds_Section WHERE Selection_ID = {0}", inpSelectionID");
context.ExecuteCommand(command );

ODATA Consume Service Operation from C# ASP.NET 4.0

I am connecting to an ODATA Service via a C# ASP.NET application, which has service operations such as:
GetItems(int? itemID, double? price)
I can consume this without issues in my browser, e.g.
http://api.mycompany.com/companycatalogue/GetItems?itemID=4
I understand how to use LINQ to Entities to consume an ODATA service, but can't find a decent explanation of how to consume service operations like the one above in C#. I have made a web reference to the service in my Visual Studio solution.
So far, I have something like this for my usual consuming of the data:
using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
var result = from i in dataContext.Items select i; //just an example
//this is where I get into problems
var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}
Any pointers?
OK, got the answer:
using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities();
DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
.AddQueryOption("paramName", 6)
.AddQueryOption("paramName2", 20.5);
List<GetItemsResult> items = q.Execute().ToList();
}
This may be help for you.
This sample code used to consume the service operation in the WFC Data Service. This service operation(GetRowCount) returns the integer(int) type value. input para name is "code"
var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();
Have you tried using HttpWebRequest?

Categories

Resources