Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table that has a productCode and productRI. i would like to have the user select the productName in a dropdownlist which sends the selected value to a sproc and have the query spit out the price so that i can manipulate it in some other functions i plan on doing.

i tried to modify a bit of code to do this for me that does essentially the EXACT same thing and sad face...i'm getting back nothing and going nowhere fast.

basic idea:
user selects productCode and the code is sent to the sql table to retrieve the productRI based on the user selected productCode. i set this up as a sql output so that i can use the decimal for other stuff.

thanks for the help. i'm sure it's something silly and embarrassing as things like this usually are.

sql table: productTable
productName | productCode | RI
varchar | varchar | decimal (6,4 i think...)

sproc:
SQL
ALTER PROCEDURE dbo.getProductRI
	(
		@productCode varchar(50),
		@productRI decimal(6,4) OUTPUT
	)
AS
	SET NOCOUNT ON
	SELECT @productRI = RI
	FROM productTable
	WHERE productCode = @productCode


materialDataBLL
C#
public class materialDataBLL
{
    public string productCode;
    public decimal pRIOut;
    public decimal pISOOut;
    public decimal pEFOut;
    public decimal pDRIOut;

	public materialDataBLL()
	{
    }

    public void materialData()
    {

        string connSTR = ConfigurationManager.ConnectionStrings["connStr"].ToString();

        //create new connSTR
        SqlConnection conn = new SqlConnection(connSTR);


        //create new sqlcomm and declare type as sproc
        SqlCommand c1 = new SqlCommand("dbo.getProductRI", conn); //RI

        SqlParameter sp = new SqlParameter("@productCode", SqlDbType.VarChar);
        sp.Size = 50;
        sp.Value = productCode;

        //declare productRefractiveIndex as output
        SqlParameter pRI = new SqlParameter("@productRefractiveIndex", SqlDbType.Decimal);
        pRI.Direction = ParameterDirection.Output;

        pRI.Precision = 6;
        pRI.Scale = 4;
        c1.Parameters.Add(sp);
        c1.Parameters.Add(pRI);

        conn.Open();
        c1.ExecuteNonQuery();

        pRIOut = Convert.ToDecimal(pRI.SqlValue.ToString());
        conn.close();


codebehind:
C#
//the line below takes the ddl selected value and sets the lbo.productCode property
        lBO.productCode = productDDL.SelectedValue.ToString();

        //retrieves material/product parameters based on selected material...
        materialDataBLL mdBLL = new materialDataBLL();
        mdBLL.productCode = lBO.productCode;
        mdBLL.materialData();


things to note...i look in the variables and i see that the productCode is set in there. so that seems fine. my output for sure is null and i'm unsure of what i'm doing wrong. any help is greatly appreciated
Posted
Updated 27-Jan-14 9:05am
v2
Comments
bowlturner 13-Jan-14 16:15pm    
Happen to have the dbo.getProductRI code available?

1 solution

Change SqlParameter pRI = new SqlParameter("@productRefractiveIndex", SqlDbType.Decimal)
to SqlParameter pRI = new SqlParameter("@productRI", SqlDbType.Decimal)
--
I do apologise for misreading some of your code. I see it is a decimal so please see my example to fix your issue.

StoredProc:
SQL
create procedure [dbo].[getNameId]
@name varchar(256),
@rate decimal(6,4) output
as
select @rate = names.rate from Names where Name = @name


UI:
XML
<div>
        Name
        <asp:TextBox runat="server" ID="nameTextBox"></asp:TextBox>
        <br />
        <asp:Button runat="server" ID="getButton" Text="Get data"
            onclick="getButton_Click" />
        <asp:Label runat="server" ID="returnValueLabel"></asp:Label>
    </div>


Code:
protected void getButton_Click(object sender, EventArgs e)
        {
            string name = nameTextBox.Text;
            decimal rate = 0;

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["localTest"].ConnectionString))
            {
                // Set command
                SqlCommand command = new SqlCommand("dbo.getNameId", connection);
                command.CommandType = CommandType.StoredProcedure;

                // Set parameters
                SqlParameter nameParam = new SqlParameter("@name",SqlDbType.VarChar);
                nameParam.Size=256;
                nameParam.Value = name;

                SqlParameter rateParam = new SqlParameter("@rate",SqlDbType.Decimal);
                rateParam.Direction = ParameterDirection.Output;
                // Instead of rateParam.Size = 10, set the following, so it matches the SQL structure and StoPro param.
                rateParam.Precision = 6;
                rateParam.Scale = 4;

                command.Parameters.Add(nameParam);
                command.Parameters.Add(rateParam);

                // Get
                connection.Open();
                command.ExecuteNonQuery();
                rate = (decimal)rateParam.Value;

                // Set ui.
                returnValueLabel.Text = rate.ToString();
            }
        }

Note: on the code line "rate = (decimal)rateParam.Value;" you could also do:
rate = Convert.ToDecimal(rateParam.SqlValue.ToString());
You can access the param.SqlValue also.
 
Share this answer
 
v2
Comments
memberxxxxxxxxxxxxxxxxx 15-Jan-14 12:46pm    
doesn't seem to make much of a difference.

i guess a question now is...and this really makes no sense to me...
can i send in a "varchar" to a sproc to retrieve decimal values?

or better yet...can i even have decimal values as an output? do i have to pull them out as varchar and then convert? i don't see how i can even do that with the table structure as is.
njammy 16-Jan-14 4:59am    
Yes you can output types other than varchar. See my updated solution.
memberxxxxxxxxxxxxxxxxx 21-Jan-14 18:12pm    
soryr it took so long for me to get back to you thank you for the help. really appreciate it
njammy 21-Jan-14 18:24pm    
No problem, did my solution work for you? If so, please mark so other users can find it helpful or I will update it again for you.
memberxxxxxxxxxxxxxxxxx 27-Jan-14 14:56pm    
i think it's 99% of the way there. i'm expecting a value of 1.4475 but getting a value of "1" instead. any ideas why that might be?

edit: figured the last bit i had wrong. thank you very much for the help.

thank you very much for the help btw. i've edited the original post to reflect the final code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900