如何在GridView中输入多个值 [英] How to enter multiple value in gridview

查看:73
本文介绍了如何在GridView中输入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何在单按钮单击中插入多个值.使用存储过程
天冬氨酸Net,C#,MySQL.
例如:gridview有3个字段. 10行,输入值后,我必须按提交按钮
必须插入值.

感谢adv
Anu

Hi,
How to insert multiple value in single button click. using stored procedure
Asp. Net, c#,mysql.
Eg: gridview having 3 field. 10 row, after entering value I have to press submit button
Value have to insert.

Thanks in adv
Anu

推荐答案

阿努,

检查此链接,可能这就是您想要的.您将需要从gridview获取数据表,然后只需调用以下方法:

Hi Anu,

Check this link, probably this is what you want. You will need to get datatable from your gridview and then just call the below method:

private static bool StoreInDataBase(DataTable dtDetails, string connectionString, string tableName)
    {
        if (dtDetails != null && dtDetails.Rows.Count > 0)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection))
                {
                    bulkcopy.DestinationTableName = tableName;
                    try
                    {
                        bulkcopy.WriteToServer(dtDetails);
                    }
                    catch (Exception ex)
                    {
                                        //Log Exception
                        return false;
                    }
                }
                connection.Close();
            }
        }
        return true;
    }




检查此链接,在数据库中存储完整的数据表




Check this link, Store complete data table in database


提交时单击
---------------------
On Submit Click
---------------------
foreach (GridViewRow rw in GridView1.Rows)
           {
               //Fill values in object
               Customers objCust=new Customers();
               objCust.Name=(TextBox)rw.Cells[0].FindControl("txtName")).Text;


               //Call StoreProcedure method for save
               saveDetails(objCust);
           }


受保护的空白Page_Load(对象发送者,EventArgs e)

如果(!Page.IsPostBack)
{
loadgridview();
}
}
私有void loadgridview()
{
字符串qry =从消息中选择*";
OdbcDataAdapter da =新的OdbcDataAdapter(qry,objcon);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
如果(e.CommandName =="InsertRecord")
{
var名称= GridView1.FooterRow.FindControl("txtname")作为TextBox;
var Email = GridView1.FooterRow.FindControl("txtemail")as TextBox;
var Message = GridView1.FooterRow.FindControl("txtmsg")作为TextBox;
OdbcCommand cmd =新的OdbcCommand();
cmd.Connection = objcon;
cmd.Parameters.AddWithValue("@ name",Name.Text);
cmd.Parameters.AddWithValue("@ Email",Email.Text);
cmd.Parameters.AddWithValue("@ Message",Message.Text);
cmd.CommandText =插入到message(Name,Email,Message)值(@ Name,@ Email,@ Message)";
objcon.Open();
cmd.ExecuteNonQuery();
objcon.Close();
loadgridview();
}
}
----------------------------
db = MYSQL
没有错误,值未插入db中,(表名=消息,字段= emp_id(增量1),名称,电子邮件,消息)
插入值时,Empid正在递增
但没有插入名称,电子邮件ID,信息.使用网格.
-
注意:
cmd.Parameters.AddWithValue("@ name",Name.Text);
通过断点,即时获取此Name.text ='anu'
的值 但是在插入qry时,即时消息并没有显示仅@name的值.
-----
另一个面团,
cmd.Parameters.AddWithValue("@ name",Name.Text);

cmd.pararmeters.add("@ name",odbctype.char);
哪个是正确的?

感谢Adv
阿努
protected void Page_Load(object sender, EventArgs e)

if (!Page.IsPostBack)
{
loadgridview();
}
}
private void loadgridview()
{
String qry = " select * from message ";
OdbcDataAdapter da = new OdbcDataAdapter(qry, objcon);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "InsertRecord")
{
var Name = GridView1.FooterRow.FindControl("txtname") as TextBox;
var Email = GridView1.FooterRow.FindControl("txtemail") as TextBox;
var Message = GridView1.FooterRow.FindControl("txtmsg") as TextBox;
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = objcon;
cmd.Parameters.AddWithValue("@name", Name.Text);
cmd.Parameters.AddWithValue("@Email", Email.Text);
cmd.Parameters.AddWithValue("@Message", Message.Text);
cmd.CommandText = " insert into message(Name,Email,Message) values(@Name,@Email,@Message)";
objcon.Open();
cmd.ExecuteNonQuery();
objcon.Close();
loadgridview();
}
}
----------------------------
db=MYSQL
no error, value is not inserting in db, ( table name=message, field = emp_id(increment 1),name,email,message)
empid is incrementing when value is inserted
but name,email id,msg is not inserting. using grid.
--
Note:
cmd.Parameters.AddWithValue("@name", Name.Text);
through break point im getting value to this Name.text = ''anu''
but in insert qry im not getting value for just @name is displaying.
-----
another dought,
cmd.Parameters.AddWithValue("@name", Name.Text);
or
cmd.pararmeters.add("@name",odbctype.char);
which is correct?

Thanks in Adv
Anu


这篇关于如何在GridView中输入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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