将复选框中的数据添加到数据库中 [英] Add Data from check box into Database

查看:58
本文介绍了将复选框中的数据添加到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设
我正在使用带有复选框的gridview,
&在gridview页脚中的按钮..
我想在按钮上单击
插入数据库中的数据 1 ..当我选中3个复选框时,应在按钮上单击,将数据保存在数据库中

2 ..当我选中1个复选框时,应单击按钮将数据保存在数据库中


需要帮助,因为我是asp.net的新手

在此先感谢
Pratham

Suppose
I am having gridview with checkboxes,
& button in footer of gridview ..
I want to insert the data in database on button click
1..when i check 3 check boxes that data should be saved in database on button click
OR
2..when i check 1 check boxes that data should be saved in database on button click


Need help as i am new to asp.net

Thanks in advance
Pratham

推荐答案

在您的保存按钮中将其放置.
in your save button place this.
for (int i = 0; i <= Gridview.Rows.Count - 1; i++) {
	GridViewRow row = Gridview.Rows(i);

	//For Each checked row In Gridview
	bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
	if (isChecked) {
		InsertRow();
		//code to insert row

	}
}


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    SqlConnection con = new SqlConnection();
    string strPath = string.Empty;
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
            con.Open();
            cmd = new SqlCommand("usp_subbrokerMapping", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 100000;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            con.Close();
            DataGrid1.Visible = true;
            Label1.Visible = true;
            Label1.Text = "Data Displayed Successfully";
        }

    }
    protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
        con.Open();
        int i = 0;
        bool blnSelect = false;
        foreach (GridViewRow row in DataGrid1.Rows)
        {
            CheckBox rb = (CheckBox)row.FindControl("RowSelector");
            Label lbltagcode = (Label)row.FindControl("lbltagcode");

            TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
            if (rb.Checked)
            {
       
               
                    cmd = new SqlCommand("usp_subbrokerMapping", con);
                    cmd.CommandType = CommandType.StoredProcedure;        
                    cmd.CommandTimeout = 100000;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
                    cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
                    cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
                    cmd.ExecuteNonQuery();            
                  
                    Label1.Visible = true;
                    Label1.Text = "Data Inserted Successfully";
                 }
                



                rb.Checked = false;
                i++;
                
               


            }
            con.Close();
        }
       
       }


使用系统;
使用System.Data;
使用System.Configuration;
使用System.Collections;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.Data.SqlClient;
使用System.IO;
使用System.Text;

公共局部类_Default:System.Web.UI.Page
{
SqlDataAdapter da =新的SqlDataAdapter();
DataTable dt = new DataTable();

SqlConnection con =新的SqlConnection();
字符串strPath = string.Empty;
SqlCommand cmd =新的SqlCommand();

受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{

SqlConnection con =新的SqlConnection(数据源= 192.168.0.141;初始目录= mis;持久性安全信息=真实;用户ID = sa;密码= ms");
con.Open();
cmd =新的SqlCommand("usp_subbrokerMapping",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da =新的SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@ tag",SqlDbType.VarChar).Value ="1";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text =数据显示成功";
}

}
受保护的void DataGrid1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{

}
受保护的void DataGrid1_RowDataBound(对象发送者,GridViewRowEventArgs e)
{

}
受保护的无效btnsave_Click(对象发送者,EventArgs e)
{
SqlConnection con =新的SqlConnection(数据源= 192.168.0.141;初始目录= mis;持久性安全信息=真实;用户ID = sa;密码= ms");
con.Open();
int i = 0;
bool blnSelect = false;
foreach(DataGrid1.Rows中的GridViewRow行)
{
CheckBox rb =(CheckBox)row.FindControl("RowSelector");
标签lbltagcode =(Label)row.FindControl("lbltagcode");

TextBox Rmcode =(TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
如果(rb.Checked)
{


cmd =新的SqlCommand("usp_subbrokerMapping",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da =新的SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@ Sub_code",SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
cmd.Parameters.AddWithValue("@ rm_code",SqlDbType.VarChar).Value = Rmcode.Text.ToString();
cmd.Parameters.AddWithValue("@ tag",SqlDbType.VarChar).Value ="2";
cmd.ExecuteNonQuery();

Label1.Visible = true;
Label1.Text =数据已成功插入";
}




rb.Checked = false;
i ++;




}
con.Close();
}

}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

SqlConnection con = new SqlConnection();
string strPath = string.Empty;
SqlCommand cmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "1";
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
DataGrid1.Visible = true;
Label1.Visible = true;
Label1.Text = "Data Displayed Successfully";
}

}
protected void DataGrid1_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{

}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.141;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
con.Open();
int i = 0;
bool blnSelect = false;
foreach (GridViewRow row in DataGrid1.Rows)
{
CheckBox rb = (CheckBox)row.FindControl("RowSelector");
Label lbltagcode = (Label)row.FindControl("lbltagcode");

TextBox Rmcode = (TextBox)DataGrid1.FooterRow.FindControl("txtRmcode");
if (rb.Checked)
{


cmd = new SqlCommand("usp_subbrokerMapping", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 100000;
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@Sub_code", SqlDbType.VarChar).Value = lbltagcode.Text.ToString().Trim();
cmd.Parameters.AddWithValue("@rm_code", SqlDbType.VarChar).Value = Rmcode.Text.ToString();
cmd.Parameters.AddWithValue("@tag", SqlDbType.VarChar).Value = "2";
cmd.ExecuteNonQuery();

Label1.Visible = true;
Label1.Text = "Data Inserted Successfully";
}




rb.Checked = false;
i++;




}
con.Close();
}

}


这篇关于将复选框中的数据添加到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆