Saima Maheen
Saima Maheen

Reputation: 132

How can I show the relationship based SQL Server table value in label in C#

I have more than two SQL Server tables which have relationships. But when I want to show two columns in label it gives me an error. I want to show Aurthorname and Catagoryname where book id is equal to textbox.text:

SqlConnection con = new SqlConnection("Data Source=SWEETHOME\\SQLEXPRESS;Integrated Security=True");
con.Open();
SqlCommand coo = new SqlCommand("Select Book.BookId , Book.Booktitle, "+ 
                         "Aurthor.Aurthorname, Catagory.Catagoryname, Status.Status, "+
                         "Book.IssuingDate, Book.ReceivingDate, Book.Issuedby " + 
                         "From  Aurthor INNER JOIN Book ON Aurthor.Aurthorid = Book.Aurthorid "+
                         "INNER JOIN Catagory ON Book.Catagoryid = Catagory.Catagoryid INNER " +
                         "JOIN Status ON Book.Statusid = Status.Statusid Where Book.Bookid = '" +
                         textBox2.Text + "'", con);
SqlDataReader koo = coo.ExecuteReader();

while (koo.Read())
{
    label20.Text = koo["Aurthor.Aurthorname"].ToString();
    label21.Text = koo["Catagoryname.Catagory"].ToString();
}

enter image description here

enter image description here

Upvotes: 0

Views: 510

Answers (1)

Steve
Steve

Reputation: 216293

You don't need to prefix the column name with the tablename. Just use

label20.Text = koo["Aurthorname"].ToString();
label21.Text = koo["CatagoryName"].ToString();

By the way, you have switched the tablename with the column name for the "Category" field

Upvotes: 5

Related Questions