Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hello guys,
below is my sql select statement :
SQL
SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount,
Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint
FROM Product, Monthlysales
where Product.P_ID = Monthlysales.P_ID

in product table there are product id, name, lead time, safety stock column, meanwhile in monthly sales table, there are productid, month (in int), and totalsalespermonth.

the problem is :

i want to retrieve the reorder point result from the select statement above in visual studio c#
the interface will include product id , month options, button and label to show the result.
the reorder point result will be based on the month, for example
if the user choose
pid 1 : 1
month options: 2 (february)
then label result will show the reorder point amount for pid 1 in february.

i also wondering whether i can directly calculate the reorder point result based on product id and its month in sql server and directly call it in c#.

how could i do it?? please help me
thank you
Posted
Updated 10-May-14 18:01pm
v2

1 solution

you need to use parameters in your where condition as below
SQL
SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount,
Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint
FROM Product, Monthlysales
where Product.P_ID = Monthlysales.P_ID AND Product.P_ID =@P_ID AND Monthlysales.Month =@Month 


C# code:

C#
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlCommand command = new SqlCommand(
    "SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount," +
    "Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint " +
    "FROM Product, Monthlysales "+
    "where Product.P_ID = Monthlysales.P_ID AND Product.P_ID =@P_ID AND Monthlysales.Month =@Month ", connection))
    {

        command.Parameters.Add(new SqlParameter("P_ID", pid));
        command.Parameters.Add(new SqlParameter("Month", month));
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        // now you have the data in Dataset ds
    }
}
 
Share this answer
 
v2
Comments
hahaahahaahahhahahahhahaha 11-May-14 0:44am    
@damithSL thank you for ur reply but i got error when i execute it as below
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@P_ID"
DamithSL 11-May-14 0:47am    
you need to write C# code for get the data from database. you can use above sql statement and set the parameters in that code. do you need help for that?
hahaahahaahahhahahahhahaha 11-May-14 1:09am    
yapp! wah thank youu. it works~ thank you so much for ur help

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