从Gridview插入选定值到Mysql数据库

本文关键字:Mysql 数据库 Gridview 插入 | 更新日期: 2023-09-27 18:12:13

我正在一个项目上工作,我有一个gridview和一个单选按钮在gridview和我想要的是,我想发送选定的行值数据库上的按钮的点击,但我得到一个错误作为对象引用不设置为对象的实例。正如我的代码Cs代码上点击按钮

 protected void btn_selectgridview_Click(object sender, EventArgs e)
{
    int k = 0;
    //Checkther whether atleast one check box is selected or not
    for (int i = 0; i <=gvrepair_details.Rows.Count-1; i++)
    {
        GridViewRow row = gvrepair_details.Rows[i];
        RadioButton rb = (RadioButton)row.FindControl("CheckBox1");
        if (rb.Checked == true)
        {
            k++;
        }
    }
    if (k == 0)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(),Guid.NewGuid().ToString(), "<script language=JavaScript>alert('select the value in grid');</script>");
        return;
    }
    for (int i = 0; i <=gvrepair_details.Rows.Count-1; i++)
    {
        string bookname = gvrepair_details.Rows[i].Cells[1].Text;
        string categoryname = gvrepair_details.Rows[i].Cells[2].Text;
        string subcategory =gvrepair_details.Rows[i].Cells[3].Text;
        string shelf_no = gvrepair_details.Rows[i].Cells[4].Text;
        string isbn = gvrepair_details.Rows[i].Cells[5].Text;
        string edition = gvrepair_details.Rows[i].Cells[6].Text;
        string status = gvrepair_details.Rows[i].Cells[7].Text;
        GridViewRow row = gvrepair_details.Rows[i];
        RadioButton rb = (RadioButton)row.FindControl("CheckBox1");
        if (rb.Checked == true)
        {
            InsertData(bookname, categoryname, subcategory, shelf_no, isbn, edition, status);
        }
    }
 }
void InsertData(String bookname, String categoryname, String subcategory,String shelf_no,String isbn,String edition,String status)
{
    try
    {
        sql = "insert into library_repair(bookname, categoryname, subcategoryname, shelf_no, isbn, edition, status)values('" +bookname+ "','" +categoryname+ "','" +subcategory+ "','"+shelf_no+"','"+isbn+"','"+edition+"','"+status+"')";
        ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}   

从Gridview插入选定值到Mysql数据库

我重新定义了你的第一个函数(代码可能包含语法错误,因为没有运行它):

    protected void btn_selectgridview_Click(object sender, EventArgs e)
    {
        int k = 0;
        int checkBoxColumnIndex = 2; //Here you should specify the cell containing the checkBox
        foreach (GridViewRow item in gvrepair_details.Rows)
        {
            RadioButton rb = (RadioButton)item.Cells[checkBoxColumnIndex].FindControl("CheckBox1");
            if (rb != null && rb.Checked) //Check if rb is null
            {
                k++;
                string bookname = item.Cells[1].Text;
                string categoryname = item.Cells[2].Text;
                string subcategory = item.Cells[3].Text;
                string shelf_no = item.Cells[4].Text;
                string isbn = item.Cells[5].Text;
                string edition = item.Cells[6].Text;
                string status = item.Cells[7].Text;
                InsertData(bookname, categoryname, subcategory, shelf_no, isbn, edition, status);
            }
        }
        //Checkther whether atleast one check box is selected or not
        if (k == 0)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>alert('select the value in grid');</script>");
            return;
        }
    }

注意checkBoxColumnIndex。应提供CheckBox1索引

try this

if (((RadioButton)gvrepair_details.Rows[i].FindControl("CheckBox1")).Checked == true)
        {
           k++;
        }
不是

if (rb.Checked == true)
        {
          k++;
        }