Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Posted
Comments
Supriya Srivastav 23-Jan-12 1:38am    
Where will you enter values,Are you using Itemtemplates for that
Explain more about your problem.

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:

C#
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
 
Share this answer
 
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);
           }
 
Share this answer
 
v2
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900