Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:56:11+00:00 2026-06-03T07:56:11+00:00

I just updated the StudentID and StudentName into the database. I use these following

  • 0

I just updated the StudentID and StudentName into the database.

I use these following codes to see value in the textbox but when i want to select the row it should be ID row not StudentID.

I mean i want to see value with StudentID, not ID row.

I don’t want to use the ID row to see the StudentName.

I want to see StudentName when i enter StudentID.

sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
adapter = new SqlDataAdapter("select * from Entry",sql);
dt = new DataTable();
adapter.Fill(dt);
textBox1.Text = dt.Rows[3]["StudentName"].ToString();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:56:13+00:00Added an answer on June 3, 2026 at 7:56 am

    If studentID is primary key of table, then use:

    DataRow row = dt.Rows.Find(studentID);
    if (row != null)
        textBox1.Text = row["StudentName"].ToString();
    

    Otherwise use dt.Select method.
    Btw mixing data access code with UI code is not very good idea

    UPDATE: also you can use LINQ

    string name = (from row in dt.AsEnumerable()
                  where row.Field<int>("StudentID") == studentID
                  select row.Field<string>("StudenName"))
                  .Single();
    

    UPDATE: If you are entering student id and want to get student name, then you can just retrieve student name from database, passing parameter to sql command:

    private string GetStudentName(int studentID)
    {
        string connString = @"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string query = "SELECT StudentName FROM Entry WHERE StudentID = @studentID";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID;
            conn.Open();
            return (string)cmd.ExecuteScalar();
        }
    }
    

    Consider also returning only first entry (if StudentID is not PK) and verifying for DbNull.

    UPDATE: If you need to retrieve several attributes of student, then I’d created class Student:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Grade { get; set; }
    }
    

    And filled its properties from data reader:

    private Student GetStudent(int studentID)
    {
        string connString = @"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string query = "SELECT * FROM Entry WHERE StudentID = @studentID";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
    
            if (!reader.Read())
                 throw new Exception("Student not found");
    
            return new Student()
            {
                Id = (int)reader["StudentID"],
                Name = (string)reader["StudentName"],
                Grade = (string)reader["Grade"]
            };
        }
    }
    

    Then when you enter student id in text box, retrieve student from database and show its properties in controls:

    int studentID = Int32.Parse(idTextBox.Text);
    Student student = GetStudent(studentID);
    nameTextBox.Text = student.Name;
    gradeTextBox.Text = student.Grade;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just updated my django to 1.4. But I am getting the following error
I just updated Rails 3.0.10 to 3.1 and everything seems to be ok. But
We've just updated ASP.NET from Preview 3 to Preview 5 and we've run into
I just updated PHP 5.2.0 to 5.2.11 and now I am getting loads of
Having just updated Mono to 2.6.3 (on OS X), I noticed in the installer
I just updated an app from .NET 2.0 to .NET 4.0 and I have
I just updated my OS from Snow Leopard to LION 10.7.3, and I usually
I just updated to Rev 17 SDK Tools and Rev 11 SDK Platform-tools and
I've just updated to the latest build of cruisecontrol.net (1.5.0.5972). I've a few teething
I have just updated os x to 10.6.3 and no Eclipse won't start the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.