Click here to Skip to main content
15,896,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a form in which there is 1 label starting date along with respective textbox.i have inserted the value in the database through ADD button (using insert query).I have taken datagrid view to display the data .The problem is now i want that when i select one row of datagridview then it should display data in respective textbox. i have tried following code but not able to get desired result for date field

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True");
          con.Open();

          SqlCommand com = new SqlCommand("select * from Publicity_Threshold  where Threshold_id=@Threshold_id='" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + "'", con);
          SqlDataReader dr = com.ExecuteReader();
          if (dr.HasRows)
          {
              dr.Read();
              txtThresholdID.Text = dr[0].ToString();
              cboThreholdname.Text = dr[1].ToString();
              cbomethodid.Text = dr[2].ToString();
              cbomediaused.Text = dr[3].ToString();
             startingdate.Value = dr[4].ToString();                    }
          dr.Close();
          con.Close();
      }

error is in this only
startingdate.Value = dr[4].ToString();
can u rectify me
Posted
Comments
Mahendra.p25 3-May-11 8:07am    
if it is label then try this startingdate.Text= dr[4].ToString();
Ankit Rajput 3-May-11 8:12am    
What is the error?
Please specify
shivani 2013 3-May-11 8:32am    
i dont get specified date in the text box which i inserted in database.i am always getting current date
Ankit Rajput 3-May-11 8:36am    
Try the solution which i have provided.

if startingdate is a datetimepicker try this:

startindate.Value = dr[4];
 
Share this answer
 
Hi,

If your control is DateTimePicker then use this syntax

startingdate.Value = Convert.ToDateTime(dr[4].ToString());


Try this code

Regards
AR
 
Share this answer
 
Comments
shivani 2013 3-May-11 8:38am    
thanx
shivani 2013 3-May-11 8:47am    
u give solution to every problem of mine.thanx
regards
try with this

C++
startingdate.Text = dr[4].ToString();
 
Share this answer
 
Comments
shivani 2013 3-May-11 8:11am    
But date value gets affected not text.I mean date is always shown in value not in text
Ankit Rajput 3-May-11 8:13am    
What is type of control(startingdate)?
shivani 2013 3-May-11 8:37am    
i got it

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