Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent accessibility

Tags:

c#

winforms

I am getting the following error

Inconsistent accessibility: parameter type 'Db.Form1.ConnectionString' is less accessible than method 'Db.Form1.BuildConnectionString(Db.Form1.ConnectionString)'

//Name spaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
 using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
 using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using System.Data.OleDb;
using System.IO;
using System.Drawing.Printing;

 //
  namespace Db
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }
     public void SetBusy()
    {
        this.Cursor = Cursors.WaitCursor;
        Application.DoEvents();
    }
    public void SetFree()
    {
        this.Cursor = Cursors.Default;
        Application.DoEvents();
    }
   //connection string into parts
    struct ConnectionString
    {
        public string Provider;
        public string DataSource;
        public string UserId;
        public string Password;
        public string Database;
    }
  //Declare
    public string BuildConnectionString(ConnectionString connStr) ------> getting error here
    {
        string[] parts = new string[5];
        parts[0] = "Provider=" + connStr.Provider;
        parts[1] = "Data Source=" + connStr.DataSource;
        parts[2] = "User Id=" + connStr.UserId;
        parts[3] = "Password=" + connStr.Password;
        parts[4] = "Initial Catalog=" + connStr.Database;
        return string.Join(";", parts);
    }
    // settings 
    public bool IsValidConnectionForPrinting()
    {
        SetBusy();

        ConnectionString connStr = new ConnectionString();
        connStr.Provider = cboProvider.Text;
        connStr.DataSource = cboDataSource.Text;
        connStr.UserId = txtUserId.Text;
        connStr.Password = txtPassword.Text;
        connStr.Database = cboDatabase.Text;
       //connection string to database 
        string connectionString = BuildConnectionString(connStr);
        OleDbConnection conn = new OleDbConnection(connectionString);
        try
        {
            conn.Open();
            OleDbCommand cmd = conn.CreateCommand;
            cmd.CommandType = CommandType.TableDirect;
            cmd.CommandText = "vw_pr_DL";
            cmd.ExecuteScalar();
            cmd.CommandText = "vw_pr_VR";
            cmd.ExecuteScalar();
            //cmd.CommandText = "vw_pr_VR"
            //cmd.ExecuteScalar()
            conn.Close();
        } 
   //Exception messages
        catch (Exception ex)
        {
            SetFree();
            if (ex.Message.StartsWith("Invalid object name"))
            {
                MessageBox.Show(ex.Message.Replace("Invalid object name", "Table or view not found"), "Connection Test");
            }
            else
            {
                MessageBox.Show(ex.GetBaseException().Message, "Connection Test");
            }
            return false;
        }
        SetFree();
        return true;
    }

// when user click testbutton

private void btnConnTest_Click(object sender, EventArgs e)
{

if (IsValidConnectionForPrinting())
    {
         MessageBox.Show("Connection succeeded", "Connection Test");
    }
}
like image 324
user1312412 Avatar asked Apr 09 '12 06:04

user1312412


2 Answers

As described here, classes and structs are private by default if no access modifier is specified. Where you have defined your struct as:

struct ConnectionString
{
    public string Provider;
    public string DataSource;
    public string UserId;
    public string Password;
    public string Database;
}

you need to instead define it as:

public struct ConnectionString
{
    public string Provider;
    public string DataSource;
    public string UserId;
    public string Password;
    public string Database;
}
like image 168
Malice Avatar answered Oct 23 '22 13:10

Malice


Your ConnectionString type is not public, yet, a public method of a public class is using it as a parameter. You must also make the type public, for instance:

// If it's a class
public class ConnectionString { ... }
like image 35
Coincoin Avatar answered Oct 23 '22 12:10

Coincoin