none
when select DatePrint it give me null if it not have record how to solve RRS feed

  • Question

  • if no record for user no in excel file database then select dateprint give me null

    so that how to solve this problem

     public string maxDate(string UserNo)
            {
    
                OleDbConnection con = new OleDbConnection(connection);
                string str = $"SELECT max(DatePrint) FROM[Sheet1$] WHERE UserNo = '{UserNo}'";
                OleDbCommand com = new OleDbCommand();
                com = new OleDbCommand(str, con);
                con.Open();
                string userno = Convert.ToString(com.ExecuteScalar());
                con.Close();
                return userno;
    
    
            }

    under button click 

      label11.Text = QRC.maxDate(textBox1.Text);

    Wednesday, February 22, 2017 11:07 PM

All replies

  • You can easily detect null results using an if instruction:

      string md = QRC.maxDate(textBox1.Text);
      if(md == null)
      {
          . . .
      }

    Or use if with the result of ExecuteScalar.

    It is up to you how to proceed in case of null dates.

    Null results can be also replaced on SQL level using ISNULL function.


    • Edited by Viorel_MVP Thursday, February 23, 2017 6:21 AM
    Thursday, February 23, 2017 6:18 AM
  • Hi eng-ahmed-salah,

    Thank you for posting here.

    According to your question is more related to ADO, I will move it to ADO.NET Managed Providers forum for suitable support.

    The Visual C# discuss and ask the C# programming language, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 23, 2017 7:16 AM
  • Hi eng-ahmed-salah,

    Since you use ExecuteScalar method, which executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

    it will return the first column of the first row in the result set, it will return a null reference if the result set is empty.

    As Viorel said, you could add a judgement in your code, like this:

    public string maxDate(string UserNo)
            {
                OleDbConnection con = new OleDbConnection(connection);
                string str = $"SELECT max(DatePrint) FROM[Sheet1$] WHERE UserNo = '{UserNo}'";
                OleDbCommand com = new OleDbCommand();
                com = new OleDbCommand(str, con);
                con.Open();
    
                string userno = Convert.ToString(com.ExecuteScalar());
    if (string.IsNullOrEmpty(userno ))
    {
    userno = "set a default string as you want";
    }
    
                con.Close();
                return userno;
    
            }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, February 23, 2017 9:05 AM
    Moderator