简体   繁体   English

为什么这不起作用? 不执行代码?

[英]Why is this not working? Does not execute code?

I am trying to have a textBox auto complete with values from a database. 我正在尝试使用数据库中的值自动完成textBox。 When i bullet test the code will walk though until the if statement in the function below. 当我进行项目符号测试时,代码将遍历直到以下函数中的if语句。 Could some one please tell me why my code wont execute after that if ? 可能有人请告诉我为什么我的代码不会之后执行的if

I am trying to run the following but it keeps jumping out before the if : 我正在尝试运行以下命令,但是在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. 在控制台上放一个消息框或一个断点。WriteLine,看看发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM