Why is this not working? Does not execute code?-Collection of common programming errors
I am trying to have a textBox auto complete with values from a database. When i bullet test the code will walk though until the if
statement in the function below. Could some one please tell me why my code wont execute after that if
?
I am trying to run the following but it keeps jumping out before the if
:
public AutoCompleteStringCollection AutoCompleate(string dataBase, string procedure)
{
AutoCompleteStringCollection namesCollection =
new AutoCompleteStringCollection();
SqlDataReader dReader;
SqlCommand cmd = new SqlCommand(procedure, con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
dReader = cmd.ExecuteReader(); // This is the last place my bullet check
// gets before it hop's out!
if (dReader.HasRows == true)
{
while (dReader.Read())
namesCollection.Add(dReader["SystemUser"].ToString());
}
con.Close();
dReader.Close();
return namesCollection;
}
Additional information, but please focus on the above!
Please ask if you need any info. 🙂
The Call to the above:
textBoxUser.AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxUser.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxUser.AutoCompleteCustomSource =
DatabaseService.Instance.AutoCompleate("AuditIT", "AutoCompleate");
Stored Proc:
USE [AuditIT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Allow textboxs to have a autocomplete
ALTER procedure [dbo].[AutoCompleate]
as
Select distinct SystemUser from SchemaAudit
order by SystemUser asc
-
There is an unhandled exception when you try to hit the database. Try to refactor the code as this:
try{ if (dataBase.Length > 0) { procedure = dataBase + ".." + procedure; } //Set procedure to DBNAME..ProcedureName SqlDataReader dReader; SqlCommand cmd = new SqlCommand(procedure, con); cmd.CommandType = CommandType.StoredProcedure; con.Open(); dReader = cmd.ExecuteReader(); // This is the last place my bullet check gets before it hop's out! if (dReader.HasRows == true) { while (dReader.Read()) namesCollection.Add(dReader["SystemUser"].ToString()); } con.Close(); dReader.Close(); } catch(Exception e) { // handle the exception better than me :) Console.WriteLine(e.Message); }
Put a messagebox or a breakpoint on the console.WriteLine and see what’s happen.