frtools
asked on
Executing a Stored Procedure in a Winform
Trying to execute a stored procedure but the only data returned is an integer and it is always 0 false? The stored proc works in management studio when executed but it doesn't seem to work in the windows form. How do I use a stored procedure in a windows form to return data not an integer.
string param1 = textBox1.Text;
string param2 = comboBox1.Text;
//Call to the Stored procedure this reutrns a value (0)
var y = dc.GetInventoryByAnyValue( param1, param2);
//Code I've been using to test the rest of the program
var q = (from item in dc.GetTable<MainInventory> ()
where item.InventoryNumber.Conta ins(param1 )
select new InventoryListResult
{
AWNum = item.AWInvNum ,
InvNum =item.InventoryNumber,
InvLocation = item.InventoryLocation,
Available = Convert.ToInt32(item.Avail able),
_upc = item.UPC,
_sku = item.SKU,
Image1 = item.ImageURL1,
Image2 = item.ImageURL2}).ToList<In ventoryLis tResult>() ;
//Stored Procedure
USE [INVENTORY]
GO
/****** Object: StoredProcedure [dbo].[GetInventoryByAnyVa lue] Script Date: 09/14/2010 12:36:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetInventoryByAnyVa lue] @search varchar (75) , @category varchar(50)
AS
SET NOCOUNT ON
DECLARE @str nvarchar(2255)
SELECT @str = 'SELECT * ' + 'FROM MainInventory WHERE ' + @category + ' LIKE @search'
set @search = '%'+@search+'%'
EXEC sp_executesql @str, N'@search varchar(50)', @search
string param1 = textBox1.Text;
string param2 = comboBox1.Text;
//Call to the Stored procedure this reutrns a value (0)
var y = dc.GetInventoryByAnyValue(
//Code I've been using to test the rest of the program
var q = (from item in dc.GetTable<MainInventory>
where item.InventoryNumber.Conta
select new InventoryListResult
{
AWNum = item.AWInvNum ,
InvNum =item.InventoryNumber,
InvLocation = item.InventoryLocation,
Available = Convert.ToInt32(item.Avail
_upc = item.UPC,
_sku = item.SKU,
Image1 = item.ImageURL1,
Image2 = item.ImageURL2}).ToList<In
//Stored Procedure
USE [INVENTORY]
GO
/****** Object: StoredProcedure [dbo].[GetInventoryByAnyVa
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetInventoryByAnyVa
AS
SET NOCOUNT ON
DECLARE @str nvarchar(2255)
SELECT @str = 'SELECT * ' + 'FROM MainInventory WHERE ' + @category + ' LIKE @search'
set @search = '%'+@search+'%'
EXEC sp_executesql @str, N'@search varchar(50)', @search
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What I suspect is cos you're binding the results to DataGridView you may have defined the binding against the DataReader. The resultset returned by SQLReader may not contain required columns as defined in the DataGridView binding.
ASKER
There was an error in the Stored Proc-- missing a space---Aghhh
This is not fair as I suggest you to use SQLCommand and others get the points?
>>This is not fair as I suggest you to use SQLCommand and others get the points?<<
You may want to double check that, it looks like you got points.
You may want to double check that, it looks like you got points.
Yes I've got Assisted Points but the idea to use SQLCommand was mine and I believe I deserve more points unless the Author thinks otherwise.
>> I believe I deserve more points unless the Author thinks otherwise.<<
Relax. This too shall pass and tomorrow will be a brighter day. :)
Relax. This too shall pass and tomorrow will be a brighter day. :)
ASKER
SQLEXCEPTION was Unhandled
Invalid column name 'InventoryNumber'.
Invalid column name 'AWInvNum'.
Invalid column name 'InventoryNumber'.
Invalid column name 'Available'.
Invalid column name 'InventoryLocation'.
Invalid column name 'ImageURL1'.
Added code---
string param1 = textBox1.Text;
string param2 = comboBox1.Text;
SqlConnection objConn = new System.Data.SqlClient.SqlC
objConn.ConnectionString = "server=SHPPING_COREI7\\SQ
SqlCommand objCmd = new SqlCommand("GetInventoryBy
objCmd.CommandType = CommandType.StoredProcedur
objCmd.Parameters.AddWithV
objCmd.Parameters.AddWithV
objConn.Open();
dataGridView2.DataSource = objCmd.ExecuteNonQuery();
objConn.Close();