【问题标题】:Input string was not in a correct format for radio button单选按钮的输入字符串格式不正确
【发布时间】:2016-12-26 05:02:02
【问题描述】:

我使用了 2 个控件在里面编辑 gridview。它显示错误“输入字符串的格式不正确”

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_grd();
                fill_gender();
            }    
        }  

        public void fill_gender()
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("usp_Gender_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                radiolist.DataValueField = "Gid";
                radiolist.DataTextField = "Gname";
                radiolist.DataSource = ds;
                radiolist.DataBind();
            }

            con.Close();
        }

        public void fill_grd()
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("usp_Login_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                grd.DataSource = ds;
                grd.DataBind();
            }
            else
            {

                grd.DataSource = null;
                grd.DataBind();
            }
            con.Close();
        }

        protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grd.EditIndex = e.NewEditIndex;
            fill_grd();
        }

        protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grd.DataKeys[e.RowIndex].Value.ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Login_delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Lid", id);
            cmd.ExecuteNonQuery();
            con.Close();
            fill_grd();
        }

        protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            TextBox TB1 = (TextBox)grd.Rows[e.RowIndex].FindControl("edittxtname");
            RadioButtonList RBL1 = (RadioButtonList)grd.Rows[e.RowIndex].FindControl("editradiolist");

            int id = int.Parse(grd.DataKeys[e.RowIndex].Value.ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Login_update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Lid", id);
            cmd.Parameters.AddWithValue("@Name", TB1.Text);
            cmd.Parameters.AddWithValue("@Gender", RBL1.SelectedValue);
            cmd.ExecuteNonQuery();
            con.Close();
            grd.EditIndex = -1;
            fill_grd();
        }

        protected void grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grd.EditIndex = -1;
            fill_grd();
        }

        protected void savebtn_Click(object sender, EventArgs e)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("usp_Login_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            cmd.Parameters.AddWithValue("@Gender", int.Parse(radiolist.SelectedValue));
            cmd.ExecuteNonQuery();
            con.Close();
            fill_grd();
        }
    }
}

【问题讨论】:

  • 该代码与单选按钮无关;请发布产生该错误的实际代码。

标签: c# sql asp.net


【解决方案1】:

不确定您的错误发生在哪里,您粘贴的代码确实与您的问题无关,但我还是会尝试回答。

您需要检查单选按钮的数据类型,它可能不是 bool 或 int。您似乎正在根据从数据库获得的结果设置数据类型,我猜您需要通过检查存储的过程 usp_Login_get 和您为单选按钮列设置的数据类型来更改数据类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2013-08-22
    相关资源
    最近更新 更多