仅执行Else条件而不执行if条件

时间:2017-05-02 05:50:19

标签: c#

private void ifsc_btn_Click(object sender, EventArgs e)
{            
        string query = "select * from ifsc where branch='" + branch_txt + "'";
        OleDbConnection conn = new OleDbConnection(conString);//connection string already defined
        OleDbCommand cmd = new OleDbCommand(query, conn);

        try
        {
            conn.Open();
            OleDbDataReader myReader;  //declared myReader
            myReader = cmd.ExecuteReader();//executes query
            if(!myReader.Read())
            {
                string scode = myReader.GetString(myReader.GetOrdinal("ifsc_code"));//get the ifsc code from database
                code.Text = scode;
            }
            else
            MessageBox.Show("No Data Found!");  //error message while no data found              
            conn.Close();
            myReader.Close();  //closing both connection and myreader
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);// exception
        }
}

2 个答案:

答案 0 :(得分:0)

问题出在这一行

string scode = myReader.GetString(myReader.GetOrdinal("ifsc"));

您指的是表名而不是列名 将“ifsc”更改为相应的列名

答案 1 :(得分:0)

我认为您应该将if语句更改为:

如果(myReader.Read())

没有使用它!标志。