Hi,
This is Sabir, I have put my code below to display the contents of textbox in a gridview and display the multiple rows added to GV in a single button event in the database table. I am able to display multiple rows in GV but not able to display them in a single button event into the database table.
Regards
Sabir.J.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing.Design;
using System.Text;
using System.Collections.Specialized;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void BindGrid(int rowcount)
{
//SqlConnection conn = new SqlConnection("Data Source=DSSSYS1\\SQLEXPRESS;Initial Catalog=Sabir;Integrated Security=True");
//conn.Open();
//SqlCommand cmd = new SqlCommand("select * from sales where ProductName='" + TextBox1.Text + "'", conn);
//SqlDataReader dr1;
//dr1 = cmd.ExecuteReader();
////dr.Read();
//if (dr1.Read())
//{
// TextBox2.Text = Convert.ToString(dr1[2]);
// TextBox3.Text = Convert.ToString(dr1[4]);
// TextBox1.Text = Convert.ToString(dr1[1]);
//}
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductID", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(String)));
//dt.Columns.Add(new System.Data.DataColumn("LotNo", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Qty", typeof(String)));
//dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Total", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dr[3] = TextBox4.Text;
//dr[4] = Ssprc.Text;
//dr[5] = Sstotal.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dr[3] = TextBox4.Text;
//dr[4] = Ssprc.Text;
//dr[5] = Sstotal.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string field1 = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
field1 = GridView1.Rows[i].Cells[0].Text; // get the field to be Inserted
sc.Add(field1); // add the field to be Inserted in the StringCollection
}
InsertRecords(sc); // call method for insert and pass the StringCollection values
}
private void InsertRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection("Data Source=DSSSYS15;Initial Catalog=SabirJ;Integrated Security=True");
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
string sqlStatement = "insert into salesdet(productid,productname,quantity,total) values()";
sb.AppendFormat("{0}'{1}';", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}