单击新按钮时,公司ID必须自动增加一个 [英] company id must be auto increment one when i click the new button

查看:65
本文介绍了单击新按钮时,公司ID必须自动增加一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



My code as follows

 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;
    }
}







客户注册页面代码如下




当我点击下拉列表中的新按钮我想要公司ID autoincrementc0001时,我有一个按钮叫新。



我的新按钮代码如下






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

 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

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'	



以上两个错误在下面的行显示如下



cmd =新的SqlCommand(str,GFun);



请帮帮我。



我的代码中有什么问题。



问候,

Narasiman P.


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.

推荐答案

你为什么想知道?



Why are you wondering?

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





SqlCommand构造函数没有重载会接受你的对象作为参数为什么要这样?



顺便说一下:永远不要让客户端计算自动增量值,将其放在数据库端。请参阅: http://www.w3schools.com/sql/sql_autoincrement.asp [ ^ ]


cmd = new SqlCommand(str, GFun);





in这一行使用你的sql连接而不是全局连接..因为当你没有输入任何方法的那些参数的准确参数或数据类型时你会弹出错误。



如果你想在每次点击中增加你的公司ID,那么非常简单...



只需创建一个方法,你可以在从company_id列获取MAX值的sql查询,并通过将其转换为int将该值保存在任何int变量中。然后在该int变量中创建一个增量。



喜欢:





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:

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();
    }


这篇关于单击新按钮时,公司ID必须自动增加一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆