mohsen.nsb44
شنبه 18 بهمن 1393, 14:05 عصر
سلام
من از طریق کد زیر اطلاعات رو از طریق دیتاتیبل و سیشن داخل گریدویو میریزم
میخوام اطلاعات اونو در بانک ذخیره کنم اما خطای زیر داده میشه
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
از کد زیر استفاده کردم
if (Session["dt"] == null)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("ردیف");
DataColumn dc2 = new DataColumn("نام محصول");
DataColumn dc3 = new DataColumn("تعداد");
DataColumn dc4=new DataColumn("قیمت جزء");
DataColumn dc5 = new DataColumn("قیمت کل ");
DataColumn dc6 = new DataColumn("کد محصول");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Rows.Add(new object[] { i, title, body,price,priceKol,cod });
Session["dt"] = dt;
GridView3.DataSource = dt;
GridView3.DataBind();
for (int j = 0; j < GridView3.Rows.Count; j++)
{
GridView3.Rows[j].Cells[0].Text = (j + 1).ToString();
GridView3.Rows[j].Cells[4].Text = (long.Parse(GridView3.Rows[j].Cells[3].Text) * int.Parse(GridView3.Rows[j].Cells[2].Text)).ToString();
}
}
else
{
DataTable dtTemp = (DataTable)Session["dt"];
dtTemp.Rows.Add(new object[] { i, title, body,price,priceKol,cod });
Session["dt"] = dtTemp;
GridView3.DataSource = dtTemp;
GridView3.DataBind();
for (int j = 0; j < GridView3.Rows.Count; j++)
{
GridView3.Rows[j].Cells[0].Text = (j + 1).ToString();
GridView3.Rows[j].Cells[4].Text = (long.Parse(GridView3.Rows[j].Cells[3].Text) * int.Parse(GridView3.Rows[j].Cells[2].Text)).ToString();
}
}</td>
</tr>
</tbody></table></div>
حال برای ذخیره از کد زیر استفاده کردم
<div class="cms_table"><table class="cms_table"><tbody><tr class="cms_table_tr" valign="top"><td class="cms_table_code">for (int i = 0; i < GridView3.Rows.Count; i++)
{
SqlConnection con2 = new SqlConnection();
con2.ConnectionString = System.Configuration.ConfigurationManager.Connecti onStrings["cn"].ConnectionString;
SqlCommand cmd2 = new SqlCommand("Sp", con2);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@CodeM",long.Parse( GridView3.Rows[i].Cells[5].Text)); }</td>
</tr>
</tbody>
من از طریق کد زیر اطلاعات رو از طریق دیتاتیبل و سیشن داخل گریدویو میریزم
میخوام اطلاعات اونو در بانک ذخیره کنم اما خطای زیر داده میشه
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
از کد زیر استفاده کردم
if (Session["dt"] == null)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("ردیف");
DataColumn dc2 = new DataColumn("نام محصول");
DataColumn dc3 = new DataColumn("تعداد");
DataColumn dc4=new DataColumn("قیمت جزء");
DataColumn dc5 = new DataColumn("قیمت کل ");
DataColumn dc6 = new DataColumn("کد محصول");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Rows.Add(new object[] { i, title, body,price,priceKol,cod });
Session["dt"] = dt;
GridView3.DataSource = dt;
GridView3.DataBind();
for (int j = 0; j < GridView3.Rows.Count; j++)
{
GridView3.Rows[j].Cells[0].Text = (j + 1).ToString();
GridView3.Rows[j].Cells[4].Text = (long.Parse(GridView3.Rows[j].Cells[3].Text) * int.Parse(GridView3.Rows[j].Cells[2].Text)).ToString();
}
}
else
{
DataTable dtTemp = (DataTable)Session["dt"];
dtTemp.Rows.Add(new object[] { i, title, body,price,priceKol,cod });
Session["dt"] = dtTemp;
GridView3.DataSource = dtTemp;
GridView3.DataBind();
for (int j = 0; j < GridView3.Rows.Count; j++)
{
GridView3.Rows[j].Cells[0].Text = (j + 1).ToString();
GridView3.Rows[j].Cells[4].Text = (long.Parse(GridView3.Rows[j].Cells[3].Text) * int.Parse(GridView3.Rows[j].Cells[2].Text)).ToString();
}
}</td>
</tr>
</tbody></table></div>
حال برای ذخیره از کد زیر استفاده کردم
<div class="cms_table"><table class="cms_table"><tbody><tr class="cms_table_tr" valign="top"><td class="cms_table_code">for (int i = 0; i < GridView3.Rows.Count; i++)
{
SqlConnection con2 = new SqlConnection();
con2.ConnectionString = System.Configuration.ConfigurationManager.Connecti onStrings["cn"].ConnectionString;
SqlCommand cmd2 = new SqlCommand("Sp", con2);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@CodeM",long.Parse( GridView3.Rows[i].Cells[5].Text)); }</td>
</tr>
</tbody>