简体   繁体   中英

No mapping exists from object type System.Web.UI.WebControls.DropDownList to a known managed provider native type

I am trying to store GridView values into a database but i keep getting error:

No mapping exists from object type System.Web.UI.WebControls.DropDownList to a known managed provider native type.

Interface: The Attendance column is a template I inserted into the grid view

X

Below is my code c#:

for (int i = 0; i < GVAttend.Rows.Count; i++) {
    SqlConnection Con = new SqlConnection();
    Con.ConnectionString = "Data Source=(local);Initial Catalog=Data;Integrated Security=True";

    String query2 = "Insert into Attendance(Student_ID, Course_Code, Date, Status, Student_Name, Att_by) Values(@Stu_ID ,@Course_Code ,@Date ,@Status ,@Stu_Name ,@Attby)";
    SqlCommand Cmd = new SqlCommand(query2, Con);

    Cmd.Parameters.AddWithValue("@Stu_ID", GVAttend.Rows[i].Cells[0].ToString());
    Cmd.Parameters.AddWithValue("@Course_Code", DropCourse.SelectedItem.Text);
    Cmd.Parameters.AddWithValue("@Date", Calendar1.SelectedDate.Date.GetDateTimeFormats()[8].ToString());
    Cmd.Parameters.AddWithValue("@Stu_Name", GVAttend.Rows[i].Cells[1].ToString());
    Cmd.Parameters.AddWithValue("@Status", GVAttend.Rows[i].Cells[2].FindControl("DropAtt") as DropDownList);
    Cmd.Parameters.AddWithValue("@Attby", Session["Username"].ToString());

    Con.Open();
    Cmd.ExecuteNonQuery();

    Con.Close();
}
   Cmd.Parameters.AddWithValue("@Status", GVAttend.Rows[i].Cells[2].FindControl("DropAtt") as DropDownList); 

looks fishy

you should get selected value from dropdownlist

 Cmd.Parameters.AddWithValue("@Status", (GVAttend.Rows[i].Cells[2].FindControl("DropAtt") as DropDownList).SelectedItem.Value)

Searching a control by column index is very fragile. It will throw run-time exception if you accidentally move columns.

foreach (GridViewRow row in GVAttend.Rows)
{
    ...
    var dropAtt = row.FindControl("DropAtt") as DropDownList;
    Cmd.Parameters.AddWithValue("@Status", dropAtt.SelectedValue);    
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM