内联网格视图更新

时间:2015-08-11 19:38:40

标签: c# sql asp.net gridview

当我在GridView中单击更新时,我收到一条错误消息

  

输入字符串的格式不正确

我很确定我输入了正确的字符串。我不确定代码有什么问题。请指教。

protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {

    bool isUpdated = false;
    //getting key balue, row id
    int subjectID = Convert.ToInt32(GridView3.DataKeys[e.RowIndex].Value.ToString());
    //getting row field details
    TextBox Assigned = (TextBox)GridView3.Rows[e.RowIndex].FindControl("Assigned");
    TextBox Location = (TextBox)GridView3.Rows[e.RowIndex].FindControl("Location");
    TextBox Notes = (TextBox)GridView3.Rows[e.RowIndex].FindControl("Notes");

    using (SqlConnection sqlCon = new SqlConnection())
    {
        using (SqlCommand cmd = new SqlCommand())
        {

            //added "@" for continuous tring in new line    
            cmd.CommandText = @"UPDATE Inventory SET Assigned=@Assigned, Location=@Location, Notes=@Notes";
            cmd.Parameters.AddWithValue("@Assigned", Assigned.Text);
            cmd.Parameters.AddWithValue("@Location", Location.Text);
            cmd.Parameters.AddWithValue("@Notes", Notes.Text);
            cmd.Connection = sqlCon;
            sqlCon.Open();
            isUpdated = cmd.ExecuteNonQuery() > 0;
            sqlCon.Close();
        }//end using
    }//end using

    if (isUpdated)
    {
        status.Visible = true;
        status.Text = "" + Assigned.Text + "Data added successfully!";
        status.ForeColor = System.Drawing.Color.Green;
    }//end if

    else
    {
        status.Visible = true;
        status.Text = "Error while updating" + Assigned.Text + "data details";
        status.ForeColor = System.Drawing.Color.Red;

    }//end else

    GridView3.EditIndex = -1;
    BindData();

}//end row updating

错误来自

  

int subjectID = Convert.ToInt32(GridView3.DataKeys [e.RowIndex] .Value.ToStrong());

谢谢!

0 个答案:

没有答案