Hi all,
Hoping you can help me finish this coding.
I have a sql database and a C# application with a datagridview.
I have the coding all working correctly for data to populate the datagridview and once I have the populated datagridview I need to save the value of the cells in the colAddress column, which contains addresses.
I have dataconnection to the sql database and the database contains to columns which are added to when I run my application button click event.
So far it will enter the records, but does not record the actual value of each cell. Instead it will display enter the datetime stamp correctly then enter the Address column as False, instead of the actual value in the cell eg. 123 Main Street,. So the records in my database display as:
colAddress createdDate
False 2012-02-24 00:00:00.000
False 2012-02-24 00:00:00.000
False 2012-02-24 00:00:00.000
False 2012-02-24 00:00:00.000
False 2012-02-24 00:00:00.000
False 2012-02-24 00:00:00.000
Here is my coding so far, how can I alter this code to get the result that I am after?
private void button5_Click(object sender, EventArgs e)
{
if (dgv.Rows.Count <= 0)
{
MessageBox.Show("No List to save.", "Empty Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (dgv.Rows.Count >= 1)
{
DialogResult dr = MessageBox.Show("Are you sure you want to save the data", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
List<DataGridViewRow> rows_w_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dgv.Rows)
{
if (Convert.ToBoolean(row.Cells[colPrint.Name].Value) == true)
{
rows_w_checked_column.Add(row);
int x;
DateTime createdDate;
createdDate = Convert.ToDateTime(DateTime.Now.ToString("dd-MMM-yyyy"));
SqlConnection frsCon = new SqlConnection();
frsCon.ConnectionString = "Data Source=DEVSERVER;Initial Catalog=FRS;Integrated Security=True";
SqlDataAdapter frsApt = new SqlDataAdapter();
SqlCommandBuilder frsCmdBlr = new SqlCommandBuilder();
DataTable frsDGV = new DataTable();
BindingSource frsBS = new BindingSource();
SqlCommand frsCommand = new SqlCommand("INSERT INTO tblAddress (colAddress, createdDate) VALUES (@colAddress, @createdDate)", frsCon);
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.Selected;
frsCommand.Parameters.Add("@createdDate", SqlDbType.DateTime).Value = createdDate;
frsCon.Open();
x = frsCommand.ExecuteNonQuery();
frsCon.Close();
}
}
}
}