Hi
I hope u can help me...
i have a code that connect to mysql db and it works fine.

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data.Types;



namespace MySqlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "localhost";
            string database = "articles";
            string user = "root";
            string password = "08031976";
            string strSQL = "SELECT * FROM users";

            string strProvider = "Data Source=" + host + ";Database=" + database + ";User ID=" + user + ";Password=" + password;
            try
            {
                MySqlConnection mysqlCon = new MySqlConnection(strProvider);
                mysqlCon.Open();

                if (mysqlCon.State.ToString() == "Open")
                {
                    Console.WriteLine("Database Connection Open");
                    Console.WriteLine("------------------------");
                    
                    MySqlCommand mysqlCmd = new MySqlCommand(strSQL,mysqlCon);
                    MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();

                    Console.WriteLine("Id\tName\tLastName");
                    Console.WriteLine("------------------------");

                    while (mysqlReader.Read())
                    {
                        Console.WriteLine(mysqlReader.GetInt32(0) + "\t" + mysqlReader.GetString(1) + "\t" + mysqlReader.GetString(2));
                    }

                }

                mysqlCon.Close();

            }
            catch (Exception er)
            {
                Console.WriteLine("An Error Occured" + er.Message);
            }

            Console.ReadKey();
        }
    }
}

now i am trying to make this code into web service but without a succsess.
I keep getting this error :
Error 1 'WebService3.Service1.Main(string[])': not all code paths return a value.
this is the problematic code.

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace WebService3
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string Main(string[] args)
        {
            string host = "localhost";
            string database = "articles";
            string user = "root";
            string password = "08031976";
            string strSQL = "SELECT * FROM users";

            string strProvider = "Data Source=" + host + ";Database=" + database + ";User ID=" + user + ";Password=" + password;
            try
            {
                MySqlConnection mysqlCon = new MySqlConnection(strProvider);
                mysqlCon.Open();
                

                if (mysqlCon.State.ToString() == "Open")
                {
                    Console.WriteLine("Database Connection Open");
                    Console.WriteLine("------------------------");

                    MySqlCommand mysqlCmd = new MySqlCommand(strSQL, mysqlCon);
                    MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
                   

                    Console.WriteLine("Id\tName\tLastName");
                    Console.WriteLine("------------------------");

                    while (mysqlReader.Read())
                    {
                        Console.WriteLine(mysqlReader.GetInt32(0) + "\t" + mysqlReader.GetString(1) + "\t" + mysqlReader.GetString(2));
                        
                    }

                }

                mysqlCon.Close();

               

            }
            catch (Exception er)
            {
                Console.WriteLine("An Error Occured" + er.Message);
            }

            Console.ReadKey();
        }
    }
}

Please Help ...

That is because you're not returning a value. There are two possible fixes here.

public string Main(string[] args)

The highlighted section of the code means that it is expecting for you to return a string value. So we can return a string value or change the declaration to indicate it has no return value. I'm not sure if [WebMethod]'s must have a return type or not, you will have to check.


1) Change the web method to a void return type:

public void Main(string[] args)
{
...code here...

2)

[WebMethod]
        public string Main(string[] args)
        {
            string host = "localhost";
            string database = "articles";
            ....more code...
            return "some string";
        }

thank's sknake :)

i want to call this web service from another web application.
so i will call him, but how i tell the ws to output the result of the query?

I'm not sure. I haven't used web services before. You may try marking this thread solved and creating another thread in the ASP.NET forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.