none
(Sumber: milis DOTNET) Error : 'SILA.C_TestConn.isKoneksiOk(string, string, string, string)': not all code paths return a value RRS feed

  • Pertanyaan

  • Dear Rekans;

    Saya benar-benar baru di C# , pindahan dari .net.

    Mohon bantuannya dan terima kasih sebelumnya.

    Begini:

    Saya punya kelas yang ingin mengembalikan nilai True atau False dari sebuah state koneksi.

    Saya sudah buat kelasnya, dan errornya tinggal 1 yaitu : “not all code paths return a value”

    Error terjadi pada : public Boolean isKoneksiOk

    Untuk fungsi StrCon saya buat public, karena kelak StrCon ini akan saya pakai lagi.

    Mohon konfirmasi , kalau di VB .net khan ada modul, C# tidak ada modul ya?

    Atas bantuan rekan – rekan saya ucapkan banyak terima kasih.

    Contoh koding terlampir di bawah ini :

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Data.SqlClient;

    namespace SILA

    {

        class C_TestConn

        {

            public Boolean isKoneksiOk(string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                Boolean Koneksi = false;

               SqlConnection oConnTest = new SqlConnection();

               oConnTest.ConnectionString = StrConn(NamaDB , NamaServer , NamaUser , Password );

               if (oConnTest.State != System.Data.ConnectionState.Open)

                {

                    oConnTest.Open();

                }

                if (oConnTest.State == System.Data.ConnectionState.Open)

                {

                    Koneksi = true;

                    return Koneksi;

                }

            }

            public string StrConn (string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                string StringKoneksi = "workstation id=" + NamaServer.Trim() + ";packet size=4096;user id=" + NamaUser.Trim() + ";data source=" + NamaServer.Trim() + ";persist security info=True;initial catalog=" + NamaDB.Trim() + ";password=" + Password.Trim() + ";Connection Timeout=0;";

                return StringKoneksi;

            } 

        }

    }


    Best Regards,
    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 29 November 2012 07.44
    Moderator

Jawaban


  • yang ini

    public Boolean isKoneksiOk(string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                Boolean Koneksi = false;

               SqlConnection oConnTest = new SqlConnection();

               oConnTest.ConnectionString = StrConn(NamaDB , NamaServer , NamaUser , Password );

               if (oConnTest.State != System.Data.ConnectionState.Open)

                {

                    oConnTest.Open();

                }

     

                if (oConnTest.State == System.Data.ConnectionState.Open)

                {

                    Koneksi = true;

                    return Koneksi;

                }

     

            }


    ubah jadi
     
    public Boolean isKoneksiOk(string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                Boolean Koneksi = false;

               SqlConnection oConnTest = new SqlConnection();

               oConnTest.ConnectionString = StrConn(NamaDB , NamaServer , NamaUser , Password );

               if (oConnTest.State != System.Data.ConnectionState.Open)

                {

                    oConnTest.Open();

                }

     

                if (oConnTest.State == System.Data.ConnectionState.Open)

                {

                    Koneksi = true;     

                }

                

    return Koneksi;

            }

    Ket: letakkan return <value> nya di scope functionnya.

    Dijawab oleh: Adi


    Best Regards,
    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 29 November 2012 07.45
    Moderator

Semua Balasan


  • yang ini

    public Boolean isKoneksiOk(string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                Boolean Koneksi = false;

               SqlConnection oConnTest = new SqlConnection();

               oConnTest.ConnectionString = StrConn(NamaDB , NamaServer , NamaUser , Password );

               if (oConnTest.State != System.Data.ConnectionState.Open)

                {

                    oConnTest.Open();

                }

     

                if (oConnTest.State == System.Data.ConnectionState.Open)

                {

                    Koneksi = true;

                    return Koneksi;

                }

     

            }


    ubah jadi
     
    public Boolean isKoneksiOk(string NamaDB, string NamaServer, string NamaUser, string Password)

            {

                Boolean Koneksi = false;

               SqlConnection oConnTest = new SqlConnection();

               oConnTest.ConnectionString = StrConn(NamaDB , NamaServer , NamaUser , Password );

               if (oConnTest.State != System.Data.ConnectionState.Open)

                {

                    oConnTest.Open();

                }

     

                if (oConnTest.State == System.Data.ConnectionState.Open)

                {

                    Koneksi = true;     

                }

                

    return Koneksi;

            }

    Ket: letakkan return <value> nya di scope functionnya.

    Dijawab oleh: Adi


    Best Regards,
    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 29 November 2012 07.45
    Moderator
  • Di Cs ada juga modul, tp namanya bukan modul :-)

    modul di cs itu sama dengan static class.
    Kalo gak salah spt ini

    public static class NamaClas
    {

    }

    Dijawab oleh: Andi Yuniarto


    Best Regards,
    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 29 November 2012 07.46
    Moderator
  • Terima kasih Pak Untuk masukkannya, saya sudah tahu kenapa , diberitahu oleh Pak Adi.

    Intinya Return dari fungsi harus masuk kedalam Blok dari fungsi, bukan dari blok Logika yang terdapat dalam Fungsi.

    Pada dasarnya, apa yang saya kerjakan sudah benar, hanya miss disitu saja (2 hari cari) …. ^_^


    Best Regards,
    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Kamis, 29 November 2012 07.46
    Moderator