Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
 public class GlobalFunction
{
    public SqlConnection con = null;
    private string connectionstring = "Data Source=(local);connect timeout = 120; Initial Catalog=CRMS;Trusted_Connection=True";
    private SqlCommand cmd;
    private SqlDataReader dr;

    public string Error;

	public GlobalFunction()
	{
		
	}


    public void BindConn()
    {
        con = new SqlConnection();
        con.ConnectionString = connectionstring;
        con.Open();
    }

    public void InsertData(string SQL)
    {
        try
        {
            BindConn();
            cmd = new SqlCommand(SQL, con);
            cmd.ExecuteReader();

        }
        catch (Exception e1)
        {
            Error = e1.Message.ToString();
            
        }
    }

    public SqlDataReader ReadSql(string SQL)
    {
        con = new SqlConnection(connectionstring);
        con.Open();
        cmd = new SqlCommand(SQL, con);
        dr = cmd.ExecuteReader();
        return (dr);

    }

    public string CntString()
    {
        return connectionstring;
    }
}



Customer regisration page code as follows


I have one Button Called New when i click the New Button in the dropdownlist i want the company id autoincrement "c0001".

My New button code as follows

C#
 public partial class Customer_Registration : System.Web.UI.Page
{
    private GlobalFunction GFun = new GlobalFunction();
    private SqlDataReader Dr;
    private string sql;
    string str;
    SqlCommand cmd;
    int count;
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

 protected void btnnew_Click(object sender, EventArgs e)
    {
        str = "select count(*) from ";
        cmd = new SqlCommand(str, GFun);
        count = Convert.ToInt16(cmd.ExecuteScalar()) + 1;
        ddlCompanyid.SelectedItem.Text = "C000" + count;
    }
}


When i run my code shows error as follows

C#
The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments

Argument '2': cannot convert from 'GlobalFunction' to 'System.Data.SqlClient.SqlConnection'	

The above two error shows in below line as follows

cmd = new SqlCommand(str, GFun);

please help me.

what is the problem in my code.

Regards,
Narasiman P.
Posted
Updated 16-Nov-13 20:18pm
v2

Why are you wondering?

C#
public class GlobalFunction
...
private GlobalFunction GFun = new GlobalFunction();
...
cmd = new SqlCommand(str, GFun);


There is no overload of SqlCommand constructor that will accept your object as parameter Why should it?

By the way: never ever let client calculate the autoincrement value, put that on database side. See: http://www.w3schools.com/sql/sql_autoincrement.asp[^]
 
Share this answer
 
Comments
AR547 17-Nov-13 2:40am    
the best way is to handle it from clientside first...rather than creating it from DB...because if you are making an increment in DB then whatever the user can close the application when in process... :)
Zoltán Zörgő 17-Nov-13 7:07am    
Wrong in general. It is acceptably only if you can guarantee, that it is a single client doing this at a time. Imagine that two clients want to generate next id. If it is done by the client, both will generate the same value.
Yes, indeed if user cancels operation, the server side generated id will be lost forever, and there can lead to gaps. But that's an other topic. Such kind of number should be only at the end of the operation when data is actually commited. But still on server side for the same reason. There are techniques to do that.
C#
cmd = new SqlCommand(str, GFun);


in this line use your sql connection instead of global connection.. because the error you are getting popup when you are not entering the accurate no of parameters or datatype of those parameters of any method.

Further its very simple if you want to make an increment in your COMPANY ID in each click...

Just create a method in which you have a sql query of getting MAX value from the company_id column and save that value in any int variable by converting it into int. then create an increment in that int variable.

like:

C#
public string getMaxID()
    {
        try
        {
            OracleCommand cmd = new OracleCommand("select nvl(max(company_id)+1,1) from table", dbConn);
            dbConn.Open();
            OracleDataReader dr = cmd.ExecuteReader();
            dr.Read();
            return dr[0].ToString();
        }
        catch (OracleException)
        {
            return "";
        }
        finally
        {
            if (dbConn.State == ConnectionState.Open)
            {
                dbConn.Close();
            }
        }
    }


       int company_id;

protected void btnnew_Click(object sender, EventArgs e)
    {
        company_id = Convert.ToInt32(getMaxID());
        company_id++;
        ddlCompanyid.SelectedItem.Text = "C000" + company_id.ToString();
    }
 
Share this answer
 
Comments
AR547 17-Nov-13 2:38am    
do initialize your INT COMPANY_ID in your pageload event...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900