plz help
i hd written this code but throwing error on CheckBox chkbx = (CheckBox)row.FindControl("chkbx"); this line.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
CheckBox chkbx = (CheckBox)row.FindControl("chkbx");
if (chkbx .Checked == true )
{
con.Close();
con.Open();
com.Connection = con;
string str = "insert into EmpDetails values ('" + row.Cells[2].Value.ToString() + "','" + row.Cells[3].Value.ToString() + "')";
com.CommandText = str;
dr = com.ExecuteReader();
MessageBox.Show("Success.....");
dr.Close();
}
Gaurav JadhavPosted Mar 7, 2013, 1:17 AM
can u know how to use crystal reports in visual studio 2010 when creating windows application.
Plz reply...
Satyapriya NayakPosted Mar 7, 2013, 12:42 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Checked_record__datagridview
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
OleDbCommand com;
public Form1()
{
InitializeComponent();
}
private void btn_insert_Click(object sender, EventArgs e)
{
int i = 0;
List
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
{
ChkedRow.Add(i);
}
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Select one checkbox");
return;
}
foreach (int j in ChkedRow)
{
str = @"INSERT INTO test1 VALUES ('" + dataGridView1.Rows[j].Cells[1].Value + "', '" + dataGridView1.Rows[j].Cells[2].Value + "');";
try
{
using (OleDbConnection con = new OleDbConnection(ConnectionString))
{
using (com = new OleDbCommand(str, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show("Records successfully inserted");
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear();
DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
col1.Name = "chkcol";
col1.HeaderText = "CheckBox";
dataGridView1.Columns.Add(col1);
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test ";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
oledbda.Fill(ds, "test");
dataGridView1.DataMember = "test";
dataGridView1.DataSource = ds;
}
}
}
Gaurav JadhavPosted Mar 7, 2013, 12:01 AM
The code u provided is worked fine bt it'll throw an error saying column name is not found.
Actually the table and columns are present in database.
no issue in the connection.
Everything worked fine except that error.
Plz suggest any idea.
Satyapriya NayakPosted Mar 6, 2013, 8:36 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Checked_record__datagridview
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
OleDbCommand com;
public Form1()
{
InitializeComponent();
}
private void btn_insert_Click(object sender, EventArgs e)
{
int i = 0;
List
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
{
ChkedRow.Add(i);
}
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Select one checkbox");
return;
}
foreach (int j in ChkedRow)
{
str = @"INSERT INTO test1(Did,Dname) VALUES ('" + dataGridView1.Rows[j].Cells["Did"].Value + "', '" + dataGridView1.Rows[j].Cells["Dname"].Value + "');";
try
{
using (OleDbConnection con = new OleDbConnection(ConnectionString))
{
using (com = new OleDbCommand(str, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show("Records successfully inserted");
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear();
DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
col1.Name = "chkcol";
col1.HeaderText = "CheckBox";
dataGridView1.Columns.Add(col1);
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test ";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
oledbda.Fill(ds, "test");
dataGridView1.DataMember = "test";
dataGridView1.DataSource = ds;
}
}
}