Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Newbie to asp.net. I am trying to get Course by Id when use click on specific Course the page navigate to another page where its only shows the specific Course by its ID. I have my GetCourse by ID method below.
I have also created object. my question is i have no clue how to pass parameter in that object to get specific course by its ID.

C#
//Get a Course by ID
    public static Course GetCourse(int id)
    {
        Course a = new Course();
        String GetCommand = "Select ID CourseName, Description, StartDate, EndDate, CourseMode from Course" + "Where ID = @CourseID";

        SqlConnection connection = DBManager.GetSqlConnection();
        SqlCommand command = new SqlCommand(GetCommand, connection);
        command.Parameters.AddWithValue("@CourseID", id);

        try
        {
            var reader = command.ExecuteReader();

            //Read the Command Object and then return details
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    a.ID = Convert.ToInt32(reader["ID"]);
                    a.CourseName = reader["CourseName"].ToString();
                    a.Description = reader["Description"].ToString();
                    a.StartDate = DateTime.Parse(reader["StartDate"].ToString());
                    a.EndDate = DateTime.Parse(reader["EndDate"].ToString());


                    switch (a.CourseMode)
                    {
                        case CourseModeOfDelivery.ClassRoom:
                            a.CourseMode = CourseModeOfDelivery.ClassRoom;
                            break;

                        case CourseModeOfDelivery.ELearning:
                            a.CourseMode = CourseModeOfDelivery.ELearning;
                            break;

                        case CourseModeOfDelivery.Online:
                            a.CourseMode = CourseModeOfDelivery.Online;
                            break;

                    }
                    a.CourseMode = (CourseModeOfDelivery) Enum.Parse(typeof(CourseModeOfDelivery),reader["CourseMode"].ToString());

                }
            }
            else
            {
                reader.Close();
            }
        }
        catch (Exception Ex)
        {

        }
        finally 
        {
            // Close and dispose
            command.Dispose();
            connection.Close();
            connection.Dispose();
        }
        return a;
    }


Code Behind:
C#
public partial class View_Course1 : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               BindCourse1();
           }
       }

       protected void BindCourse1()
       {
           this.CourseListView1.DataSource = CourseRepository.GetCourse();
           this.CourseListView1.DataBind();
       }

       protected void CourseListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
       {
           ListViewItem item = CourseListView1.Items[e.ItemIndex];
           int ID = int.Parse(CourseListView1.DataKeys[e.ItemIndex].Value.ToString());

           TextBox TCourseName = (TextBox)item.FindControl("CourseNameTextBox");
           TextBox TDescription = (TextBox)item.FindControl("DescriptionTextBox");
           TextBox TStartDate = (TextBox)item.FindControl("StartDateTextBox");
           TextBox TEndDate = (TextBox)item.FindControl("EndDateTextBox");
           DropDownList DCourseMode = (DropDownList)item.FindControl("CourseModeDdl");

           Course CoToBeUpdated = new Course();
           CoToBeUpdated.ID = ID;
           CoToBeUpdated.CourseName = TCourseName.Text.Trim();
           CoToBeUpdated.Description = TDescription.Text.Trim();
           CoToBeUpdated.StartDate = DateTime.Parse(TStartDate.Text);
           CoToBeUpdated.EndDate = DateTime.Parse(TEndDate.Text);

           switch (CoToBeUpdated.CourseMode)
           {
               case CourseModeOfDelivery.ClassRoom:
                   CoToBeUpdated.CourseMode = CourseModeOfDelivery.ClassRoom;
                   break;
               case CourseModeOfDelivery.ELearning:
                   CoToBeUpdated.CourseMode = CourseModeOfDelivery.ELearning;
                   break;
               case CourseModeOfDelivery.Online:
                   CoToBeUpdated.CourseMode = CourseModeOfDelivery.Online;
                   break;
           }

               CoToBeUpdated.CourseMode = (CourseModeOfDelivery)Enum.Parse(typeof(CourseModeOfDelivery), (DCourseMode.SelectedValue));

           CourseRepository.UpdateCourse(CoToBeUpdated).ToString();

           BindCourse1();
       }
Posted
Updated 14-Sep-15 9:34am
v2
Comments
ideafixxxer 14-Sep-15 16:30pm    
What is the actual problem? Is your code failing, throwing exceptions?
Member 11983666 14-Sep-15 18:05pm    
It throwing below exceptions
this.CourseListView1.DataSource = CourseRepository.GetCourse(1000);

when i try to run my code with CourseID inserted manually its shows error.

InvalidOperationException was unhandled by user code. An exception of type system.InvalidOperationException occurred but was not handled in user code. Data Source is an invalid type. it must be either an IListSource, IEnumerable or IDataSource.

Not sure if this is the problem but most likely your SQL statement is causing an exception. If you have a look at
C#
String GetCommand = "Select ID CourseName, Description, StartDate, EndDate, CourseMode from Course" + "Where ID = @CourseID";

In the concatenated string there is no space between the table name Course and the keyword WHERE.
Unless you're planning to add something in between, why not remove the concatenation:
C#
String GetCommand = "Select ID CourseName, Description, StartDate, EndDate, CourseMode from Course Where ID = @CourseID";


Another thing is that you have an empty catch block. So whatever errors you may encounter you throw them all away. Instead of hiding the errors, you should add proper error handling, such as informing the user, writing a log etc.

The third thing is that you should probably define the id as a parameter for GetCourse in
C#
this.CourseListView1.DataSource = CourseRepository.GetCourse(???);
if this is the same method. However, the data source should probably contain all relevant rows, not just a single one so I'm not quite sure if this is what you're after.
 
Share this answer
 
v2
Comments
Member 11983666 14-Sep-15 18:03pm    
i try to fix the space issue but still getting the error.

this.CourseListView1.DataSource = CourseRepository.GetCourse(1000);

when i try to run my code with CourseID inserted manually its shows error.

InvalidOperationException was unhandled by user code. An exception of type system.InvalidOperationException occurred but was not handled in user code. Data Source is an invalid type. it must be either an IListSource, IEnumerable or IDataSource.
Wendelius 14-Sep-15 23:46pm    
As said a data source needs to be a collection of courses, not a single course. This is the reason why I wrote that the data source should probably contain several rows.

However, if you want to return only a single course and use that as a data source, modify the method to something like

public static List<course> GetCourse(int id)

add the course to a list and in the end return the list.
Hi,

As per your thread you want to know how to pass course parameter to fetch records based on course, right..?

If your need is like above, my suggestion is better to bind all the course details in one dropdown control and based up on your dropdown selection you can bind your course details in gridview.

EX:
C#
Protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
          ListOfCourses();
          BindCourses(ddlCourse.SelectedValue.ToString());
     }
}
protected void ListOfCourses()
{
     DataSet ds=// get your all the course details from database
     ddlCourse.DataSource=ds;
     ddlCourse.DataTextField="CourseName";
     ddlCourse.DataValueField="CourseID";
     ddlCourse.DataBind();
}
protected void ddlCourse_OnselectedIndexChanged(object sender, EventArgs e)
{ 
     BindCourses(ddlCourse.SelectedValue.ToString());
}


Try, something like above to achieve your goal.

Hope this will helpful to you..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900