7 Replies - 1895 Views - Last Post: 22 March 2011 - 05:20 AM Rate Topic: -----

#1 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Putting the results of an SQL Select to seperate labels

Posted 21 March 2011 - 04:39 AM

I am trying to get the results of an SQL SELECT query, and then seperate the columns and put them into seperate labels.

my query is

Quote

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\dCoffeeInfo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand selection = new SqlCommand("SELECT * FROM tCoffee WHERE CoffeeName = '" + selected + "'", conn);

conn.Open();
selection.ExecuteNonQuery();
SelectedOrigin.Text =
conn.Close();


It should fetch this data as an example:

Quote

1 | After Dinner | Medium | Africa | 8 | 10000 | <image>


i want them spit into the following labels

Quote

<not needed> | Name Label | Strength Label | Origin Label | Price Label | Quantity Label | ImageBox


Hope that makes sense

~Jack

Is This A Good Question/Topic? 0
  • +

Replies To: Putting the results of an SQL Select to seperate labels

#2 NavilShah  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 02-November 10

Re: Putting the results of an SQL Select to seperate labels

Posted 21 March 2011 - 05:01 AM

the simpler way is to store the output into data table and then assign the values to label

for example


Label1.Text = dt.rows[0][0].ToString();
Label1.Text = dt.rows[0][1].ToString();
.
.
.
.
.
.
Labeln.Text = dt.rows[0][n].ToString(); // n no of Label




Try this it would really work....
Was This Post Helpful? 0
  • +
  • -

#3 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: Putting the results of an SQL Select to seperate labels

Posted 21 March 2011 - 01:50 PM

hang on, i should probably explain better, i am getting the contents of the "selected" variable from another page via a session...
explained better here

its coming from a grid view on the other page, into the session, then into a variable, and then used in a query to get the rest of that row...

will that still work?
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Putting the results of an SQL Select to seperate labels

Posted 21 March 2011 - 05:09 PM

Can you post the full code for what you are currently trying along with whatever errors you are getting? Just the code from where you're getting the value from session up to where you are trying to put the values into the labels should be enough.
Was This Post Helpful? 0
  • +
  • -

#5 Ninjato  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 24-February 11

Re: Putting the results of an SQL Select to seperate labels

Posted 22 March 2011 - 01:05 AM

View PostAtomika3000, on 21 March 2011 - 04:39 AM, said:

conn.Open();
selection.ExecuteNonQuery();
SelectedOrigin.Text =
conn.Close();


ExecuteNonQuery() method will only execute the query and in return will provide you with no. of rows affected, so there is no scope of fetching the data using ExecuteNonQuery() method. To fetch data from the SELECT Query use SqlDataReader, that will make you understand to show them onto Label Control.

You need to create an instance how class SqlDataReader and initialize it by calling ExecuteReader() method of SqlCommand Object.
Was This Post Helpful? 1
  • +
  • -

#6 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: Putting the results of an SQL Select to seperate labels

Posted 22 March 2011 - 03:56 AM

ok, i understand that i have to use the "ExecuteReader()" method, but i'm still not sure how to get the results from the Reader() into seperate labels. the MSDN website is a little confusing

Getting The Name From a GridView, Into a Session And Then Redirecting Page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            Session["Selected"] = row.Cells[1].Text;
            Response.Redirect("Information.aspx");
        }



This next part happens on a new page

Getting The Rest Of The Row, And Filling The Other Labels
string selected;

        protected void Page_Load(object sender, EventArgs e)
        {
            SelectedItem.Text = (string)Session["Selected"];
            selected = (string)Session["Selected"];
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=K:\dCoffeeInfo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            SqlCommand selection = new SqlCommand("SELECT * FROM tCoffee WHERE CoffeeName = '" + selected + "'", conn);

            conn.Open();

            SqlDataReader reader = selection.ExecuteReader();

            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
            }

            reader.Close();
        }



Hope this helps

This post has been edited by Atomika3000: 22 March 2011 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Putting the results of an SQL Select to seperate labels

Posted 22 March 2011 - 04:44 AM

try something like this

while (reader.Read())
{
    Label1.Text = reader["column_name1"].ToString();
    Label2.Text = reader["column_name2"].ToString();
    Label3.Text = reader["column_name3"].ToString();
}



Hopefully it's obvious, but if not, you need to change "column_name#" to the actual name of the column in your database you are wanting to put into that label. And change the names of the labels to match the labels in your application.
Was This Post Helpful? 1
  • +
  • -

#8 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: Putting the results of an SQL Select to seperate labels

Posted 22 March 2011 - 05:20 AM

Brilliant, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1