How to get records from different tables

Here is my Database diagram and my scenario is that When a user enter number of person and amount, amount/person
let suppose 4/1000=250 and now 250 is , match in Product_Price Field in RstProductDetails, and select only Restaurant where 250 is matched. Next when a user select Restaurant e.g KFC and then KFC Products details is show.
Here is my [WebMethod]

[WebMethod]
    public DataSet Restaurant(decimal amount, decimal persons)
    {
        //       DataSet result = Amount / personHash;
         //amount.ToString("amount");
        decimal price = amount / persons ;
        DataSet result = null;
        const string SQL_COMMAND_TEXT = "SELECT Product_Name,Product_Price FROM ABCD WHERE Product_Price<= @price";
        using (SqlConnection connection = Class1.GetConnection())
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(SQL_COMMAND_TEXT, connection))
            {
                command.Parameters.Add("@Rst_Name", SqlDbType.NVarChar);
                command.Parameters.Add("@Persons", SqlDbType.NVarChar);
                command.Parameters.Add("@price", SqlDbType.Int);
                command.Parameters["@Rst_Name"].Value = amount;
                command.Parameters["@persons"].Value = persons;
                command.Parameters["@price"].Value = price;
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                {
                    result = new DataSet();
                    dataAdapter.Fill(result);
                }
            }
        }

        return result;
    }




  • Edited by Ali Ashiq 16 hours 49 minutes ago
February 1st, 2014 6:40pm

Sir i have a table Product_Price(nvarchar). When i execute the query it will give me error

"Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value ' 
145' to data type int."

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2014 8:41am

Can you show your query? Product_Price is a column? Try the below

SELECT * FROM RSTProductDetails WHERE Product_Price ='250'

February 2nd, 2014 9:22am

Ali

I do not see a column named "amount"

SELECT <columns> FROM RSTName JOIN RSTProductDetails   ON RSTName.RST_Name=

 RSTProductDetails.RST_Name WHERE Product_Price=250   

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2014 9:34am

>WHERE Product_Name WHERE Product_rice<=250 = @Rst_Name"

The above does not appear to be valid.

You have to construct a valid SQL string.

February 2nd, 2014 10:50am

sir when i execute this query
SELECT * FROM RstProductDetails WHERE Product_Price <='300'

it will show all records which is less than or greater than 300. I am shocked about this, and Product_Price (nvarchar), because when we scrapped websites through scrapper code it will store automatically in the database ans the price format on different websites is e.g "Rs 600" or "Rs 1,300"   

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2014 1:39pm

>>>I am shocked about this

You need to store the prices in appropriate data type  like DECIMAL(5,2) for example. In your case SQL Server compares  strings which leads to incorrect results.

If you get different price formats I would suggest to take care on the client side and not storing it as a string .

February 3rd, 2014 1:25am

why do you store it ain string format thats the issue. Just store it as numeric/decimal and store only actual value. the currency symbols etc can be formatted based using front end formatting functions. In case you need to show multiple currency values, show currency information in seperate field and at front end use that field to determine what format you want to use to display (ie $,Rs,

etc)

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2014 5:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics