retreiving values from datatable
-
Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:
dtTableRecords_out = null;
string strQueryString = null;
SqlCommand objSqlCommand = null;
SqlConnection objSqlConnection = null;
DataSet objDataSet = null;
SqlDataAdapter objDataAdapter = null;
SqlDataReader objDataReader = null;
string day = (DateTime.Now.AddDays(1).Day.ToString());
string month = (DateTime.Now.Month.ToString());
string year = (DateTime.Now.Year.ToString());
string date_nextDay = month + '/' + day + '/' + year;try { objDataSet = new DataSet(); objDataAdapter = new SqlDataAdapter(); objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString()); strQueryString = "select \* from view\_reservation where date = " + date\_nextDay; objSqlCommand = new SqlCommand(strQueryString, objSqlConnection); objDataAdapter.SelectCommand = objSqlCommand; objSqlConnection.Open(); objDataSet.Clear(); objDataAdapter.Fill(objDataSet); objSqlConnection.Close(); dtTableRecords\_out = objDataSet.Tables\[0\]; if (strQueryString != null) { Console.WriteLine("Service is reserved for tommorow"); } else { Console.WriteLine("no reervation"); } } finally { if (objSqlConnection.State == ConnectionState.Open) { objSqlConnection.Close(); } } return strQueryString;
}
but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block
-
Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:
dtTableRecords_out = null;
string strQueryString = null;
SqlCommand objSqlCommand = null;
SqlConnection objSqlConnection = null;
DataSet objDataSet = null;
SqlDataAdapter objDataAdapter = null;
SqlDataReader objDataReader = null;
string day = (DateTime.Now.AddDays(1).Day.ToString());
string month = (DateTime.Now.Month.ToString());
string year = (DateTime.Now.Year.ToString());
string date_nextDay = month + '/' + day + '/' + year;try { objDataSet = new DataSet(); objDataAdapter = new SqlDataAdapter(); objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString()); strQueryString = "select \* from view\_reservation where date = " + date\_nextDay; objSqlCommand = new SqlCommand(strQueryString, objSqlConnection); objDataAdapter.SelectCommand = objSqlCommand; objSqlConnection.Open(); objDataSet.Clear(); objDataAdapter.Fill(objDataSet); objSqlConnection.Close(); dtTableRecords\_out = objDataSet.Tables\[0\]; if (strQueryString != null) { Console.WriteLine("Service is reserved for tommorow"); } else { Console.WriteLine("no reervation"); } } finally { if (objSqlConnection.State == ConnectionState.Open) { objSqlConnection.Close(); } } return strQueryString;
}
but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block
I think that you have made an error in your
if
condition. How canstrQueryString
benull
? Just a few lines above you set it to"select * from view_reservation where date = " + date_nextDay;
, and you never reset it! From a very quick look at your code I suspect that what you need is something like:if (dtTableRecords\_out.Rows.Count > 0) { Console.WriteLine("Service is reserved for tommorow"); } else { Console.WriteLine("no reervation"); }
Two other things that might make your code better/more secure. Please research the
using
construct, this will enable you to do things like:using (objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString())) { strQueryString = "select \* from view\_reservation where date = " + date\_nextDay; objSqlCommand = new SqlCommand(strQueryString, objSqlConnection); objDataAdapter.SelectCommand = objSqlCommand; objSqlConnection.Open(); objDataSet.Clear(); objDataAdapter.Fill(objDataSet); objSqlConnection.Close(); //<=============== delete this line, no longer required }
Using
using
ensures thatobjSqlConnection
gets closed and disposed, guaranteed, regardless of exceptions or any other problem short of a power-cut. Also research using parameterized queries Building SQL statements the way you are currently doing, is prone to all sorts of malicious exploits. Good Luck! :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:
dtTableRecords_out = null;
string strQueryString = null;
SqlCommand objSqlCommand = null;
SqlConnection objSqlConnection = null;
DataSet objDataSet = null;
SqlDataAdapter objDataAdapter = null;
SqlDataReader objDataReader = null;
string day = (DateTime.Now.AddDays(1).Day.ToString());
string month = (DateTime.Now.Month.ToString());
string year = (DateTime.Now.Year.ToString());
string date_nextDay = month + '/' + day + '/' + year;try { objDataSet = new DataSet(); objDataAdapter = new SqlDataAdapter(); objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString()); strQueryString = "select \* from view\_reservation where date = " + date\_nextDay; objSqlCommand = new SqlCommand(strQueryString, objSqlConnection); objDataAdapter.SelectCommand = objSqlCommand; objSqlConnection.Open(); objDataSet.Clear(); objDataAdapter.Fill(objDataSet); objSqlConnection.Close(); dtTableRecords\_out = objDataSet.Tables\[0\]; if (strQueryString != null) { Console.WriteLine("Service is reserved for tommorow"); } else { Console.WriteLine("no reervation"); } } finally { if (objSqlConnection.State == ConnectionState.Open) { objSqlConnection.Close(); } } return strQueryString;
}
but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block
1. if your using .net 3.5 then use linq to sql here -> http://www.asp.net/learn/linq-videos/ 2. why are you checking wherewer the strQueryString is null or nott why whuld that mather your outputting to dtTableRecords_out the first element of objDataSet.Tables[0] isent that what your going for.
-
I think that you have made an error in your
if
condition. How canstrQueryString
benull
? Just a few lines above you set it to"select * from view_reservation where date = " + date_nextDay;
, and you never reset it! From a very quick look at your code I suspect that what you need is something like:if (dtTableRecords\_out.Rows.Count > 0) { Console.WriteLine("Service is reserved for tommorow"); } else { Console.WriteLine("no reervation"); }
Two other things that might make your code better/more secure. Please research the
using
construct, this will enable you to do things like:using (objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString())) { strQueryString = "select \* from view\_reservation where date = " + date\_nextDay; objSqlCommand = new SqlCommand(strQueryString, objSqlConnection); objDataAdapter.SelectCommand = objSqlCommand; objSqlConnection.Open(); objDataSet.Clear(); objDataAdapter.Fill(objDataSet); objSqlConnection.Close(); //<=============== delete this line, no longer required }
Using
using
ensures thatobjSqlConnection
gets closed and disposed, guaranteed, regardless of exceptions or any other problem short of a power-cut. Also research using parameterized queries Building SQL statements the way you are currently doing, is prone to all sorts of malicious exploits. Good Luck! :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
thankyou people 4 ur interest Yes i usually use parameter classes for sql queries.I will use that here after the correct operation of my code. I have also tried the code of what u suggested but still output is incorerct.Although whenever i execute the query in sql it returns me some rows. I have also tried with SqlDataReader but DataReader remains null:(