3 Replies - 732 Views - Last Post: 14 September 2015 - 03:38 PM

#1 rav smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 15

how to passing parameter in object

Posted 14 September 2015 - 01:37 PM

Newbie to asp.net. I am trying to get Course by Id when user 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 where to pass parameter in the object to get specific course by its ID.

//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:

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();
       }


Is This A Good Question/Topic? 0
  • +

Replies To: how to passing parameter in object

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,894
  • Joined: 12-December 12

Re: how to passing parameter in object

Posted 14 September 2015 - 02:02 PM

I cannot really help you with this but I will point out that you need a comma after ID in your SQL statement and a space between Course and Where. You don't need to concatenate the two parts of this string, so you'll see this space more clearly if you don't.
Was This Post Helpful? 0
  • +
  • -

#3 rav smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 15

Re: how to passing parameter in object

Posted 14 September 2015 - 03:23 PM

View Postandrewsw, on 14 September 2015 - 02:02 PM, said:

I cannot really help you with this but I will point out that you need a comma after ID in your SQL statement and a space between Course and Where. You don't need to concatenate the two parts of this string, so you'll see this space more clearly if you don't.


Thanks for the reply.
i try to add comma and then try to pass CourseID manually it shows below error.
this.CourseListView1.DataSource = CourseRepository.GetCourse(1000);


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.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,894
  • Joined: 12-December 12

Re: how to passing parameter in object

Posted 14 September 2015 - 03:38 PM

Did you try to interpret the error?

Quote

Data Source is an invalid type. it must be either an IListSource, IEnumerable or IDataSource.

You are returning 'a' from your method GetCourse(). 'a' is a single Course() not a collection.

Note also that this code
                while (reader.Read())
                {
                    a.ID = Convert.ToInt32(reader["ID"]);
                    a.CourseName = reader["CourseName"].ToString();
                    a.Description = reader["Description"].ToString();

would be overwriting the same properties each time, for a single Course instance. You don't need a while loop because there should only be only record returned based on an ID. (It is not essential to change this though, as it would only iterate once if there were only a single row returned. I would change it personally, as it is misleading to read.)

Essentially, if GetCourse is only intended to return a single Course then this cannot be set as the DataSource for your ListView. If this single Course is to be the DataSource then your function could return a collection containing this single item. (This collection must implement IListSource, IEnumerable or IDataSource, which most of them do.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1