记录未保存在数据库中 [英] records not saving in database

查看:65
本文介绍了记录未保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在会话变量中存储了一些记录,并在带有数据表的gridview中显示了这些记录,但是当我提交这些记录以仅保存最后保存的记录时.

建议我,我的代码是..

当我使用添加按钮的数据表是

Hi,

I have stored some record in session variable and displaying these records in gridview with datatable but when I submit these records to save only last record saved.

suggest me, my code is..

when I use add button for datatable is

protected void btnAdd_Click(object sender, EventArgs e)
       {
           double a, b;
           a = double.Parse(txtqty.Text);
           b = a * double.Parse(LblRate1.Text);
           txtAmount.Text = b.ToString();

              dt = (DataTable)Session["Address"];
               dr = dt.NewRow();

               dr["Product_Name"] = DropDownList1.SelectedItem.Text;
               dr["Product_Quantity"] = txtqty.Text;
               dr["product_rate"] = LblRate1.Text;
               dr["Product_Amount"] = txtAmount.Text;
               dt.Rows.Add(dr);
               dt.AcceptChanges();
               GridView1.DataSource = dt;
               GridView1.DataBind();
               GridView1.Visible = true;

       }



当数据库的提交按钮是



and when submit button for database is

protected void btnSubmit_Click(object sender, EventArgs e)
        {
           grandtotal();
           insertproductid();
            try
            {
                con.Open();
                cmd = new SqlCommand("insert_order", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@customer_name", SqlDbType.NVarChar).Value = txtName.Text;
                cmd.Parameters.Add("@customer_phone", SqlDbType.NVarChar).Value = TxtPhone.Text;
                cmd.Parameters.Add("@customer_address", SqlDbType.NVarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@product_total_amount", SqlDbType.Int).Value =txtTotalAmount.Text;
                cmd.Parameters.Add("@product_quantity", SqlDbType.Int).Value =txtqty.Text;
                cmd.Parameters.Add("@product_amount", SqlDbType.Int).Value = txtAmount.Text;
                cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
            }





what will be change in this code??

推荐答案

您仅将最后一条记录插入数据库中.
为所有新记录运行一个循环,并为每个新条目调用存储过程.
You are inserting only the last record into the database.

Run a loop for all your new records and call the stored procedure for each new entry.


如果您尝试插入数据表中的所有记录,而不是在访问这些记录时进行遍历调用存储过程.在提交事件中,您将从文本框中插入值,当您单击提交"按钮时,这些值将是您网页上的最后一个值.


If you are trying to insert all the records that you have in your datatable than iterate through those records while calling the stored procedure . In your submit event you are inserting values from your textboxes which will be the last one you have on your web page when you hit the submit button.


for (int i = 0; i < GridView1.Rows.Count; i++)
            {
string Name =GridView1.Rows[i].Cells[0].Text.Trim(); 
.... // Rest of the code  like that
....

InsertMyRecord(Name,...//Other Parameters);

}



将您的SQL代码放在单独的过程中,然后将值传递给它(这不是必不可少的,只是建议使您的代码更优雅).



Put your SQL code in a separate procedure and pass values to it(This is not essential but just a recommendation to make your code bit elegant).


看看

这篇关于记录未保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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