locked
how to store a value from a database into a variable within asp.net RRS feed

  • Question

  • User-996252219 posted

    I want to access two columns in my database table and store the two results in two separate variables on asp.net.  

    I have no idea how I can access the database to retrieve the results to store them in variables.  I already have connection strings in my web config file as I am populating drop down lists and gridviews.  Just not sure how to write code to save them as variables.  I am coding in c#, any information would be great.

    Thursday, March 21, 2013 5:38 PM

Answers

  • User-917364509 posted

    Here's a sample in C# for you:

                string firstVariable = string.Empty;
                string secondVariable = string.Empty;
                try
                {
    
                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["yourconnectionstring"].ConnectionString))
                    {
                        using (SqlCommand command = new SqlCommand("SELECT Column1, Column2 FROM YourTable", connection))
                        {
                            connection.Open();
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    firstVariable = reader[0].ToString();
                                    secondVariable = reader[1].ToString();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //error handling...
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 24, 2013 10:09 AM

All replies

  • User-917364509 posted

    Here's a sample in C# for you:

                string firstVariable = string.Empty;
                string secondVariable = string.Empty;
                try
                {
    
                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["yourconnectionstring"].ConnectionString))
                    {
                        using (SqlCommand command = new SqlCommand("SELECT Column1, Column2 FROM YourTable", connection))
                        {
                            connection.Open();
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    firstVariable = reader[0].ToString();
                                    secondVariable = reader[1].ToString();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //error handling...
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 24, 2013 10:09 AM
  • User-996252219 posted

    Thank you, if I already have my connection strings in my web config file how would i write it?

    Monday, March 25, 2013 4:50 AM
  • User-917364509 posted

    Just replace "yourconnectionstring" with the name of your connection string in web.config.

    Web.config:
        <add name="yourconnectionstring" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />

    Code:
    ConfigurationManager.ConnectionStrings["yourconnectionstring"].ConnectionString

    Monday, March 25, 2013 5:09 AM
  • User-996252219 posted

    Where about would I put it in the code, I have it within the button code but it won't do anything.

     

     protected void BtnActivity_Click(object sender, EventArgs e)
            {
                String min = String.Empty;
                String max = String.Empty;

                 

         try
                {
                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PlanningPATSConnectionString"].ConnectionString))
                    {
                        using (SqlCommand command = new SqlCommand("SELECT MinimumTime, MaximumTime FROM Activites", connection))
                        {
                            connection.Open();
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    min = reader[0].ToString();
                                    max = reader[1].ToString();
                                }
                            }
                        }
                    }
                }
                catch
                {

                }

                Int32 Minium = Convert.ToInt32(min);
                Int32 Maximum = Convert.ToInt32(max);

                Int32 newTime, newHours, newMins;

                if (ddlTime.SelectedValue != "-1")
                {
                   newTime = Convert.ToInt32(ddlTime.SelectedValue);
                }
                else
                {
                   newHours = 0;
                    if (txtTime.Text != String.Empty)
                    {
                        newHours = Convert.ToInt32(txtTime.Text);
                    }

                    newMins = Convert.ToInt32(ddlMin.SelectedValue);
                    newTime = (newHours * 60) + newMins;
                }

                if (newTime < 20)
                {
                    pnlMin.Visible = true;
                }
                else if (newTime > Maximum)
                {
                    pnlMax.Visible = true;
                }

                else
                {
                    submit();
                }
            }

    Monday, March 25, 2013 5:29 AM
  • User-917364509 posted

    Are you getting an exception if you remove the try..catch? What's the error message?

    Monday, March 25, 2013 5:39 AM
  • User-996252219 posted

    Would it cause a problem since I have it as String min = String.Empty;  In my database the columns i am trying to get data from are set to ints?

    Monday, March 25, 2013 5:42 AM
  • User-996252219 posted

    Also just realised that each activity has a different minimum and maximum time, how would I write the SQL statement.  Will I need to create a variable within the c# code to get the activity value from the drop down list?

    Monday, March 25, 2013 5:56 AM