0

Im trying to create a calculator for a rental car service. Essentially I have to be able to multiply the start and end date put in by the user with the values stored in the database to get the prices. Im doing the code in visual studio and the database is done in Microsoft SQL Server. Following is the code Ive tried out but it keeps returning the lettering in the string command for the database . Could anyone tell me how i can get it to return the actual amounts please?

private void button8_Click(object sender, EventArgs e)
    {
        DateTime startdate = dateTimePicker1.Value.Date;
        DateTime enddate = dateTimePicker2.Value.Date;

        int no_of_days = ((TimeSpan)(enddate - startdate)).Days;
        int no_of_weeks = no_of_days/7;
        int no_of_months = no_of_weeks / 4;
        

        string query_week = " Select weekly_rent from Rent *no_of_weeks";
        string query_month = " Select monthly_rent from Rent *no_of_months";
        string query_days = " Select daily_rent from Rent *no_of_weeks";

        SqlCommand cmd = new SqlCommand(query_week, con);
        SqlCommand cmd1 = new SqlCommand(query_month, con);
        SqlCommand cmd2 = new SqlCommand(query_days, con);

        string total = query_week + query_month + query_days;

        label12.Text =  total.ToString();
  • 1
    The `total` variable is just concatenating the three strings you've made. You're not actually executing any of your queries. – gunr2171 Sep 15 '21 at 12:10
  • Not exactly but i did find a different way to do it, Thank you. –  Sep 17 '21 at 09:48

1 Answers1

0

Here is a method which you might need to create to read data from the Database.

    // your method to get Rent data from database 
    public static DataTable GetRentData(string sqlQueryCommand)
    {
        DataTable dataTable = new DataTable();
        string connString = @"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
        //refer https://www.connectionstrings.com/sql-server/  for setting differnt type of connection string.
        // the connection string can be initiliase in your configurations classes. I left it here so that you can test your feature.
        
        using (SqlConnection connection = new SqlConnection(connString))
        {
            SqlCommand cmd = new SqlCommand(sqlQueryCommand, connection);
            connection.Open();
            // create data adapter
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            // this will query your database and return the result to your datatable
            da.Fill(dataTable);
            connection.Close();
            da.Dispose();           
        }
        return dataTable;
    }

And after that you can call the above method in your button8_Click method to get calculated rent:

    int no_of_days = ((TimeSpan)(enddate - startdate)).Days;
    int no_of_weeks = no_of_days/7;
    int no_of_months = no_of_weeks / 4;
    
    var data = GetRentData("Select weekly_rent,monthly_rent ,daily_rent  from Rent");
    long total = Convert.ToInt32(data.Rows[0]["monthly_rent"])*no_of_months + 
        Convert.ToInt32(data.Rows[0]["weekly_rent"]) * no_of_weeks+
        Convert.ToInt32(data.Rows[0]["daily_rent"]) * no_of_days;
    
    label12.Text =  total.ToString();
  • Yes thank you, I think I would up doing something a little similar but I did it all within the same button click event. –  Sep 17 '21 at 09:51