After running foreach loop. I am getting NullReferenceException on second item, because query result is null. But I have more item to use on same query and get the result on richTextBox1. so have can I continue the foreach loop if there is any null result.
foreach (string Items in listBox4.Items)
{
using (OracleCommand crtCommand = new OracleCommand("select REGEXP_REPLACE(dbms_metadata.get_ddl('TABLE','" + Items + "'),('" + txtSrcUserID.Text + "...'),'', 1, 0, 'i') from dual", conn1))
{
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText(crtCommand.ExecuteScalar().ToString() + ";");
richTextBox1.AppendText(Environment.NewLine);
}
}
This is dangerous programming. You really should be checking for the null. Assuming your loop will get bigger.. I would include a continue and make it a bit more obvious what it is you're doing for readability. Omit the continue if you don't need it.
var result = crtCommand.ExecuteScalar();
if (result != null) {
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText(result.ToString() + ";");
richTextBox1.AppendText(Environment.NewLine);
}
else {
continue;
}
// any other code here.
Change crtCommand.ExecuteScalar().ToString() to (crtCommand.ExecuteScalar() ?? string.Empty).ToString(). This is logically equivalent to:
object dbResult = crtCommand.ExecuteScalar();
if(dbResult == null)
{
richTextBox1.AppendText(";");
}
else
{
richTextBox1.AppendText(dbResult.ToString() + ";");
}
If you want to completely ignore a null result instead of treating it as an empty string, use #SimonWhitehead's code
Related
I have recently been working with a <select> tag, however, I have noticed you cannot determine, using an if statement if the GET is NULL or equal to "".
var tbl_st = (from c in db.tblfsk_style select c).ToArray();
build.Append("<select id='style' name='style' class='styles'>");
foreach (var style in tbl_st)
{
build.Append("<option value='" + style.StyleID + "'>" + style.Description + "</option>");
}
build.Append("</select>");
if(Request.QueryString["style"] != "")
{
var choosen = Request.QueryString["style"];
var tbl_colour = (from c in db.tblfsk_style_colour where c.StyleID == choosen select c).ToArray();
build.Append("<select id='colour' name='colour' class='styles'>");
foreach (var colour in tbl_colour)
{
build.Append("<option value='" + colour.ColourID + "'>" + colour.ColourID + "</option>");
}
build.Append("</select>");
}
build.Append("<button type='submit' class='btn'>Continue</button>");
The idea is when they choose Continue as a Button the next thing loads up but I am struggling to find a way to check if the style is null or not.
I have tried:
if(Request.QueryString["style"] != "") { // next <select> tag
And:
if(!string.IsNullOrEmpty(Request.QueryString["style"])) { // next <select> tag
Is there a way to determine if the style is null in the GET?
PHP example of doing this (to explain better):
if(isset($_GET['style'])) { // next <select> tag
I fixed this by trying using the try and catch method.
try
{
var sty_in = Request.QueryString["style"].ToString();
build.Append("test");
}
catch(Exception)
{
var tbl_st = (from c in db.tblfsk_style select c).ToArray();
build.Append("<select id='style' name='style' class='styles'>");
foreach (var style in tbl_st)
{
build.Append("<option value='" + style.StyleID + "'>" + style.Description + "</option>");
}
build.Append("</select>");
}
So if it has been inputted, we can replace test with putting our new <select> statement.
Or even easier:
if(!string.IsNullOrEmpty(Request.QueryString["style"].ToString())){ }
Or
if(Request.QueryString["style"].ToString() != null) { }
I was missing the ToString()
You are using Request.QueryString so the value should be passed through the URL as a GET parameter. You can easily see the value of style if it is passed correctly through the URL string.
in debugging my wcf call its skipping the while loop and not reading the internal data. I've been bouncing through SO and msdn for help and nothing seems to stand out. Anyone have any clues as to what I'm missing or doing to cause this loop to not iterate through?
Solution: this was a DB problem, once I updated the Oracle DB values the query ran in SQL developer and debugging is working in VS2010.
Thanks
using (connection)
{
try {
connection.ConnectionString = connString;
OracleCommand command = new OracleCommand(sqlString, connection);
connection.Open();
OracleDataReader reader = command.ExecuteReader();
while (reader.Read()) {
td.TNAME = reader.IsDBNull(0) ? " " : reader.GetValue(0).ToString();
td.TASK = reader.IsDBNull(1) ? " " : reader.GetValue(1).ToString();
td.STATUS = reader.IsDBNull(2) ? " " : reader.GetValue(2).ToString();
td.COMPLETE = reader.IsDBNull(3) ? " " : reader.GetValue(3).ToString();
td.PRIORITY = reader.IsDBNull(4) ? 0 : Convert.ToInt32(reader.GetValue(4));
tl.Add(td);
}
//while (reader.NextResult());
reader.Close();
} catch (Exception ex) {
myErr = ex.Message;
}
}
return tl;
If you're actually getting to the while statement without execptions, then the obvious answer is that your datareader has no data. Try examining the value of reader.HasRows before the while loop. I imagine it will be false. Does your sql statement actually return data in SQL Developer?
There are two possible reasons for the loop to not execute:
There is an exception from the code that precedes the loop.
reader.Read() doesn't return any data.
Of course, if your loop never executes then there is no data returned from the query, but I see another problem in your code. The variable td is never re-initialized and in every loop you add to the list the same variable updated with the values extracted from the current loop values.
So you end with the value of the last loop in your tl (whatever is it)
I think you should write
while (reader.Read())
{
td = new YourTDClass(); // Create a new instance of an element of td class
td.TNAME = reader.IsDBNull(0) ? " " : reader.GetValue(0).ToString();
td.TASK = reader.IsDBNull(1) ? " " : reader.GetValue(1).ToString();
td.STATUS = reader.IsDBNull(2) ? " " : reader.GetValue(2).ToString();
td.COMPLETE = reader.IsDBNull(3) ? " " : reader.GetValue(3).ToString();
td.PRIORITY = reader.IsDBNull(4) ? 0 : Convert.ToInt32(reader.GetValue(4));
tl.Add(td);
}
I think you should write more easily:
var list = new List<dynamic>();
while(dr.Read())
{
var value = new YourClass {
propClassString = dr.GetString(0), //for string
propClassInt = dr.GetInt32(1), //for int
propClassDateTime = dr.GetDateTime(2) //for datetime
};
list.Add(value);
}
return list;
I'm getting content from a database and returning it for Ajax processing via Javascript. Pretty simple stuff. The issue here is that I can't seem to find a good way to loop through the data and the MSDN documentation is obscenely poor for its odbcreader methods.
using (OdbcCommand com = new OdbcCommand("SELECT * FROM pie_data WHERE Pie_ID = ?",
con)) {
if (Request.Form["reference_id"] == "") {
returnError();
} else {
com.Parameters.AddWithValue("", Request.Form["reference_id"]);
com.ExecuteNonQuery();
using (OdbcDataReader reader = com.ExecuteReader()) {
string finalstring = "";
while (reader.Read()) {
if(reader.HasRows) {
finalstring = reader.GetString(9) + ",";
for (int i = 0; i <= 8; i = i + 1) {
finalstring = finalstring + reader.GetValue(i).ToString() + ",";
}
finalstring = finalstring + "|";
reader.NextResult();
}
}
if (finalstring != "") {
finalstring = finalstring.Remove(finalstring.Length -1, 1);
Response.Write(finalstring);
}
}
noredirect = 1;
}
}
However, here is the sample output:
00001,0,Pie Johnson,piesaregreat#yum.com,,,10/7/2010 12:00:00 AM,Bakery,N/A,N/A,
As you can see, the second deliminator is not appearing at all, when it really should. Also, this query, when run in heidisql, returns a good number of rows, not just this one result. Once I have it passed to the Javascript, I can figure it out since I have much more experience with that and I've actually done this before via PHP.
I would use a DataTable and a DataAdapter:
String finalString;
var tblPieData = new DataTable();
using(var con = new OdbcConnection(connectionString))
using (OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM pie_data WHERE Pie_ID = ?", con))
{
da.SelectCommand.Parameters.AddWithValue("Pie_ID", reference_id);
da.Fill(tblPieData);
var rowFields = tblPieData.AsEnumerable()
.Select(r => string.Join(",", r.ItemArray));
finalString = string.Join("|", rowFields);
}
You are replacing the value of finalstring in every iteration of the loop. This will cause only the values for a single row to be returned:
finalstring = reader.GetString(9) + ",";
as well as removing the last character at the end of each iteration through the columns in the rows (the pipe, not the trailing comma as I'm expecting you want):
finalstring = finalstring.Remove(finalstring.Length -1, 1);
EDIT:
It also looks like you are skipping over every other record by both looping on reader.Read() as well as calling reader.NextResult()
This line:
finalstring = finalstring.Remove(finalstring.Length -1, 1);
is going to remove the last instance of your pipe-delimiter, so if there is only one record (which there appears to be,) you shouldn't see one.
Unless I am missing something...
EDIT
Actually, if you are looking for two records, you are probably missing the second one because you start your loop with while(reader.Read()) and end it with reader.NextResult();. These will both advance the reader, causing you to miss every other record.
EDIT 2
You are also overwriting finalstring on each iteration; you probably want to append to this (making it a StringBuilder would make the most sense for efficiency's sake, if you don't know how many records to expect.)
Your looping structure with the reader appears to have some problems, not the least of which is the reset of finalAnswer within your loop and giving you only a single result combined with using both .Read() and .NextResult:
Suggested fixes...not tested, so all standard caveats apply :) :
Edited per Mark Averius' comment and OP confirmation of original intent:
using (OdbcDataReader reader = com.ExecuteReader()) {
if (reader.HasRows())
{
string finalstring = "";
while (reader.Read()) {
finalstring = finalstring + reader.GetString(9) + ",";
for (int i = 0; i <= 8; i++) {
finalstring = finalstring + reader.GetValue(i).ToString() + ",";
}
finalstring = finalstring + "|";
}
if (finalstring != "") {
finalstring = finalstring.Remove(finalstring.Length -1, 1);
Response.Write(finalstring);
}
}
}
I want to be able to display balance to label and my code is not working. This is code I got so far:
SqlDataReader readdata;
{
sqlCommandbalance.Connection.Open();
sqlCommandbalance.Parameters["#accountID"].Value = show.accoundID;
readdata = sqlCommandbalance.ExecuteReader();
string balanceDB = null;
while (readdata.Read())
{
balanceDB = readdata["balance"].ToString();
}
sqlCommandbalance.Connection.Close();
balanceShow.Text += " " + balanceDB.ToString();
}
On this line - balanceShow.Text += " " + balanceDB.ToString(); got a error saying Object reference not set to an instance of an object.
You're calling balanceDB.ToString(), when balanceDB will be null if the data reader contains no rows.
Note that balanceDB.ToString() is redundant, since balanceDB is already a string. Just take out the ToString call.
You could also initialize balanceDB as
string balanceDB = "";
Originally you set balanceDB = null;.
If no data was found, then no assignment will be made to balanceDB in the while loop, so it stays undefined.
It's because of string balanceDB = null;
Set that to string balanceDB = string.Empty; and you should be OK
Readdata.read probably returned false, making the while to never run. Thus balanceDB is still null.
Looks like your query is returning an empty set. You should check for that before you set the balanceShow.Text:
if (balanceDB != null) {
balanceShow.Text += " " + balanceDB.ToString();
} else {
// tell the user that id: show.accoundID has no balance
}
Probably your code is returing null in dataRader and consequentily you're tanking an null value on balanceDB. I recomend you do something like this:
public static string GetBalance() {
string result = string.Emtpy;
using (var connection = /* Build your SqlConnection */) {
using (var cmd = new SqlCommand("your select command here", connection) {
try
{
connection.Open();
// you don't need a reader to take just a single value
object value = cmd.ExecuteScalar();
if (value != null)
result = value.ToString();
}
catch(Exception ex)
{
/* you've got an error */
}
finally
{
connection.Close();
}
}
}
return result;
}
and in your page:
string balance = GetBalance();
if (!string.IsNullOrEmpty(balance))
balanceShow.Text += string.Contat(" ", balance);
if you wish to concat the balanceShow label, use '+=' operator, instead of this you just '='.
PS: Sorry for my english!
Edit:
DataClassesDataContext dc = new DataClassesDataContext();
string _idCompany = Request["idCompany"];
var newes = dc.GetNewsCompany(Int64.Parse(_idCompany));
string date = "";
string newsHtml = "<center>";
if(newes.GetEnumerator().MoveNext()){
foreach (var item in newes)//say Error .......................
{
// date = calendar.GetDayOfMonth(item.DateSend) + "/" + calendar.GetMonth(item.DateSend) + "/" + calendar.GetYear(item.DateSend).ToString();
// newsHtml += "<li class='news-item'><a style='text-decoration:none' class=\"link\" onclick=\"$(\'#BodyNews\').text(\'" + HttpUtility.HtmlEncode(item.Body).Trim() + "\');$(\'#BodyNews\').dialog({resizable:false});\" href=\"#\" > " + item.Title.ToString() + "</a> " + date + " </li>";
}
newsHtml += "</center>";
}
else
{
// var propertyCompany = dc.GetPropertyCompanyById(Int64.Parse(_idCompany));
// newsHtml += "<li class='news-item'><a style='text-decoration:none' class=\"link\" );$(\'#BodyNews\').dialog({resizable:false});\" href=\"#\" > " + "!به صفحه شخصی شرکت " + propertyCompany.FirstOrDefault().NameCompany + " خوش آمدید " + "</a> " + date + " </li>";
}
return newsHtml;
say error:The query results cannot be enumerated more than once
how check var is empty or null with out enumerated;
Why bother with the if at all?
var newes = dc.GetNewsCompany(Int64.Parse(_idCompany));
//if (newes.GetEnumerator().MoveNext())//check is null or empty
var newesList = newes.ToList();
if (neweList.Count > 0)
{
...
}
You can always check the newesList.Count property afterward.
Not sure what's available as a member in newes, but if it's an object and depending on what dc.GetNewsCompany returns you could check for null
if (news == null) return;
or if it returns an empty collection/array, just check the count/length:
if (news.Count == 0) return;
if (news.Length == 0) return;
the error comes, because you are using .GetEnumerator() on newes and then using the newes again in a foreach Loop .. this causes the "double enumeration".
Generally avoid walking "such var"'s with a foreach, since the DataReader is locked the whole loop !. Means that you cannot use the same entitie in this loop.
Better .ToList() , you can the list.AsQuearable agian if you want to Linq on it
f.e. something like
var newes = dc.CompanyTable.Where(ln => ln.id.Equals(_idCompany));;
List<CompanyTable> newesList = newes.ToList();