Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
C#
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"));

                                
                                // Database connection
                                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();
                            }
                            
                        }
                        
                }
            }
Posted
Comments
graciax8 24-Feb-12 4:33am    
check the datatype of the column. is it in the right datatype? thanks.
[no name] 27-Feb-12 0:15am    
The datatype of the sql column that I am saving the data too, is a Varchar(255).

Hi Saas

This line is wrong:

C#
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.Selected;


This is why you are getting a boolean value returned. You need to replace the colAddress.Selected with value inside the selected cell.
 
Share this answer
 
Try this

C#
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.SelectedCell.Value;
 
Share this answer
 
Comments
[no name] 27-Feb-12 0:20am    
unfortunately, when entering this I do not have the option to enter SelectedCell.Value; I am currently using Visual Studio 2010 if that helps. Basically, after I enter colAddress. it only gives me the option Selected, SelectedCell is not available. If I used dgv which is the name of my datagridview then enter SelectedCells it does not give the option to enter .value
[no name] 27-Feb-12 1:34am    
Got it sort of, I have entered dgv.SelectedCells[0].Value;
which saves the value of the first cell. now need to create a loop to add 1 to the SelectedCells[0] for each time the loop goes around.
[no name] 27-Feb-12 2:41am    
how can I add 1 to the SelectedCells[0] each time the foreach loop goes around?
I have to set this up so that only the rows_w_checked_column is used and to add to the SelectedCells[number] goes through the loop. Could you please advise how I can do this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900