I've got a DateTime record during my table inside a database and that i write a question to have it in the database:
string command2 = "select Last_Modified from Company_Data where Company_Name='" + DescriptionEntryForm.SelectedItem.ToString() + "'"; SqlCommand search_company2 = new SqlCommand(command2, con_string.con); SqlDataReader company_reader2 = search_company2.ExecuteReader(); dateform.Text = company_reader2.GetValue(0).ToString(); company_reader2.Close();
However the penultimate statement throw the best saying "Invalid make an effort to read when no information is present".
How do i solve it?
Well, the immediate trouble with your code is you haven't known as
company_reader2.Read() to maneuver the cursor to the first row.
In the paperwork for [cde]:
The default position from the SqlDataReader is prior to the first record. Therefore, you have to call Read to start being able to access data.
It's also wise to note the return worth of
SqlDataReader.Read which signifies whether you've read towards the finish from the record set.
- You need to put
Read()claims round the
SqlCommand, otherwise if there's the best you will not be closing the bond. You might have the ability to pull off getting rid of the command and letting the readers just disappear instantly - however i typically get rid of both so I don't have to think too carefully.
- You are presuming the format of date/time is going to be appropriate just by calling
SqlDataReaderwithout any format specifier.
- You need to use a parameterised SQL query to prevent SQL injection attacks.
It appears like you've performed the command, but haven't really read from this.