面向对象方法中执行execute scaller的问题 [英] problem with executing execute scaller in object oriented approach

查看:91
本文介绍了面向对象方法中执行execute scaller的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分类的名称dbcon,其中有一个函数..我遇到此错误

i have a classed name dbcon in which i have a function.. i am getting the this error

Error	26	'dbcon.ohh(string)': not all code paths return a value	





public int ohh(String query)
        {
            int c; 
            try
            {
                MAconn = new SqlConnection();
                MAconn.ConnectionString = connectionString;
                MAconn.Open();

                MAcmd = MAconn.CreateCommand();
                MAcmd.CommandText = query;
                c =(int)MAcmd.ExecuteScalar();
                MAconn.Close();
                return c;


            }



我在该代码中将其用于计数查询



and i am using it for count query in that code

<pre>string query= "Select Count(*) FROM roles inner join users on roles.UserID=users.UserID inner join groups on roles.GroupID =groups.GroupID where username ='" + txtusername.Text + "' AND RoleName = '" + DropDownList1.Text + "'";
        int count = obj.ohh(query);
        if (count > 0)
        {
            Label1.Text = "record exits";
        }
        else
        {
            string query = "INSERT INTO ROLES (UserId,GroupId) SELECT UserId,GroupId FROM Users, Groups WHERE username= '" + txtusername.Text + "' AND RoleName='" + DropDownList1.Text + "'";
            bool b=obj.UDI(query);
            
            if (b)
            {
                lblresult.Text = "Inserted";
                //Response.Redirect("Products.aspx");
            }
            else
            {

                lblresult.Text = "Not Inserted";
            }

推荐答案

就像异常消息中指出的那样,并非通过您的方法的所有路径都将返回值.
现在您还没有发布整个ohh方法,因此无法确切地说出问题出在哪里.
但是我想您在尝试之后有一个catch,它不会抛出异常,因此您需要在try-catch(-finally)块之后或catch块中有一个return语句(尽管这被认为是很糟糕的)练习)
Like the exception message says not all paths through your method will return a value.
Now you haven''t posted your entire ohh method so it''s impossible to say where exactly your problem is.
But I guess that you have a catch after your try which does not rethrow the exception and therefore you''ll need to have a return statement after your try-catch(-finally) block or in the catch block (though that is considered bad practice)


这篇关于面向对象方法中执行execute scaller的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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