0

here's my code

con.Open();
SqlCommand cmd = new SqlCommand("SELECT [Date] FROM [dbo].[DS_PROJECT] ORDER BY Date ASC", con);
cmd.ExecuteNonQuery();
SqlCommand cm = new SqlCommand("SELECT [Time] FROM [dbo].[DS_PROJECT] ORDER BY Time ASC", con);
cm.ExecuteNonQuery();
con.Close();
DateTime currenttime = DateTime.Now;
DateTime userdate = Convert.ToDateTime(cmd);
DateTime usertime = Convert.ToDateTime(cm);

i want to convert the date and time values saved in my database but am unable to do it and this exception occurs

Unable to cast object of type 'System.Data.SqlClient.SqlCommand' to type 'System.IConvertible

Please Help me. Thank You

4
  • Hi Ali, ExecuteNonQuery should be used for SQL commands that modify data (e.g. Inserts, Updates, Deletes). If you want to read data you should use ExecuteReader instead. See msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx Commented Jul 22, 2018 at 8:06
  • You get data by using ExecuteReader method of a SqlCommand and take data from the data reader it returns. There is a simple example on this page. Commented Jul 22, 2018 at 8:06
  • Sorry... what??? First you execute a query as non-query. Then you try to convert the query object to a value it should return. Start here: dotnetperls.com/sqldatareader Commented Jul 22, 2018 at 8:06
  • Also, if you want to get one and only one value from a SQL query you should use ExecuteScalar method, see msdn.microsoft.com/en-us/library/… Commented Jul 22, 2018 at 8:08

3 Answers 3

1

Using ExecuteNonQuery() is intended for UPDATE, INSERT and DELETE queries that shouldn't return a value or a record set.

You can use ExecuteReader() to retrive multiple data records.

If you want to query for just a single value, you should use:

cmd.ExecuteScalar();

Note : Check for null first as it can return a a null reference if the result set is empty.

Sign up to request clarification or add additional context in comments.

Comments

0

cm.ExecuteNonQuery()

The method is guess is not correct as per

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

ExecuteNonQuery() returns the number of rows affected. Also the method is generally used when executing insert update or delete query. To get the result from the select statement, use the following blog by MSDN.

3 Comments

This is not an answer to the question, it should be a comment instead.
can you edit the code , i am trying but there i am unable to do it?
@RuiJarimba I recently joined Stackoverflow and do not have enough rewards to comment. Thanks for suggestion.
0

Normally, ExecuteNonQuery is used for the insert as well as update statement. You can use DataSet or DataReader , if you will have get multiple values from the database. Here in your case if you are using the query for single value retrieval, then you can use the ExecuteScalar menthod. which will return single value from the database. May be the below link will helpful..

http://csharp.net-informations.com/data-providers/csharp-sqlcommand-executescalar.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.