0

in my Sql Database i have

 ID Name Cost Weight
 1 John  15   36
 2 Mike  8    45
 3 Smith 10   12

i have a dropdownlist

<asp:SqlDataSource ID="MainCost" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [Name] FROM [Human]"></asp:SqlDataSource>

containg the value of the names and a button that when clicked it should multiply the Cost*Weight, how would i get the specific values to multiply? i also have a Class for DbConnctivity thanks

2 Answers 2

1

I have written a function for getting the Cost*Weight value of selected user from DropdownList. you can call that function whenever user is selected from DropDownList and Button is Clicked

Note: Please change ConnectionString and UIControl Names asper your requirement

Complete Code:

protected void Button1_Click(object sender, EventArgs e)
        {            
       int TotalCostWeight=getTotalWeightandCost();    
        }

     private   int getTotalWeightandCost()
        {
            int total = 0;
            String strCon = "Data Source=systemname;Initial Catalog=databasename;uid=uid;pwd=pwd;Integrated Security=True;";
            using (SqlConnection sqlCon = new SqlConnection(strCon))
            {
                String strCmd = "SELECT Cost*Weight as total FROM [Human] WHERE [Name] = @Name";
                using(SqlCommand sqlcommand = new SqlCommand(strCmd, sqlCon))
                sqlCon.Open();
                command.Parameters.Add("@Name", SqlDbType.VarChar).Value = DropDownList1.SelectedValue.ToString();
                SqlDataReader sReader = sqlcommand.ExecuteReader();
                if (sReader.Read())
                    total = Convert.ToInt32(sReader[0].ToString());
            }
        }
            return total;
        }
Sign up to request clarification or add additional context in comments.

Comments

0

SELECT Cost* Weight FROM [Human] WHERE [Name] = @Name

(place the string instead of the @Name or use SQL Parameters)

Example for SQL Parameter:

command.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Jhon";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.