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 6174171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:21+00:00 2026-05-23T23:42:21+00:00

When StudentId is passed as a parameter in controller action, how can I validate

  • 0

When StudentId is passed as a parameter in controller action, how can I validate if the id passed is present in StudentTable in a controller action?

        public ActionResult LookUpStudentId(string id)
    {

        if(id != //not present in StudentTable)
          return new RedirectResult("~/Error/NotFound");


        return View();

    }
  • 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-05-23T23:42:22+00:00Added an answer on May 23, 2026 at 11:42 pm

    The answer to your question will very much depend on the data access technology you are using to access your SQL server. Because you haven’t told us about it here’s how to achieve this using plain ADO.NET:

    public bool IsStudentExists(string id)
    {
        using (var conn = new SqlConnection("some connection string"))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandText = "SELECT id FROM StudentTable WHERE id = @id";
            cmd.Parameters.AddWithValue("@id", id);
            using (var reader = cmd.ExecuteReader())
            {
                return reader.Read();
            }
        }
    }
    

    and then:

    public ActionResult LookUpStudentId(string id)
    {
        if(!IsStudentExists(id))
        {
            return new RedirectResult("~/Error/NotFound");
        }
        return View();
    }
    

    Obviously this data access code is better to be refactored in a repository so that your controller is not tightly coupled to the data access technology you are using. So for example you would define a IStudentsRepository:

    public interface IStudentsRepository
    {
        Student GetStudent(string id);
    }
    

    which you would then implement and now your controller action could work with this abstraction:

    public class StudentsController: Controller
    {
        private readonly IStudentsRepository _repository;
        public StudentsController(IStudentsRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult LookUpStudentId(string id)
        {
            var student = _repository.GetStudent(id);
            if(student == null)
            {
                return new RedirectResult("~/Error/NotFound");
            }
            return View(student);
        }
    }
    

    All that is left is to configure your DI framework of choice to inject the proper implementation of this repository into the controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return StudentId to use elsewhere outside of the scope of the
I'm trying to output a string from an Object that has been passed into
I get the following errors with my code. cannot convert parameter 1 from 'std::string'
I have a function that should return the Id of the element passed in
How can I get the YUI3 sendRequest applied to a Datasource to return predefined
I have a table ClassAttendance and I'm using MSSQL 2005 studentID attendanceDate status -------------------------------------
I have a table that looks like this for about ~30 students: StudentID Course*
I am trying to access data.get_age_display in my email template. I can't seem to
My jQuery code is: $(document).ready(function(){ $('#StudentRegisterForm').validate({ rules: { email: { required:true, email:true } }
I've got a MS Access 2007 DB with records of passed and failed attempts

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.