0

I am trying to write a web method that will enabled me to INSERT text directly to the sql table via a web service. I managed to view any text entered manually to the table, on the web service xml page. Want I want to do now is to be able to insert data from the web service to the database.

The Idea behind this is, for users to be able to access view and add information/text to the database via the web service. I will appreciate any help of directions towards solving the issue in the last WebMethod (public void InsertInfo...) below.

I would like help on how to create/add a textbox that will insert data directly to the table. Or any other way of inserting information into the table via the web service.

namespace SkiFriendService
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class Service1 : System.Web.Services.WebService
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SkiFriendService.Properties.Settings.Setting"].ToString());


            [WebMethod]
            public string simpleMethod(String srt)
            {
                return "Hello "+srt;
            }


            [WebMethod]
            public int anotherSimpleMethod(int firstNum, int secondNum)
            {
                return firstNum + secondNum;
            }


            [WebMethod]
            public string[] getMessage(string from, string to) 
            {
                List<string> messages = new List<string>();
                con.Open();
                    string sqlquery = string.Format("select Message from MessageTable where Sender='{0}' and Receiver='{1}'", from, to);
                SqlCommand com = new SqlCommand(sqlquery, con);
                SqlDataReader sqlReader = com.ExecuteReader();
                while (sqlReader.Read()) 
                {
                    messages.Add(sqlReader.GetString(0));
                }
                con.Close();
                return messages.ToArray();
            }


            [WebMethod]
            public void InsertInfo(string sender, string receiver, string message)
            {
                List<string> messages = new List<string>();
                con.Open();
                string sql = "INSERT INTO MessageTable (Sender, Receiver, Message) VALUES (@Sender, @Receiver, @Message)";
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader sqlReader = com.ExecuteReader();
                while (sqlReader.Read())
                {
                    messages.Add(sqlReader.GetString(0));
                }
                con.Close();

            }





            }



            }

I hope I've been clear enough with this. I am still learning this so a lot of this is new to me. I would appreciate any help. Thank You

1 Answer 1

2
        [WebMethod]
        public void InsertInfo(...)
        {
            ...
            SqlDataReader sqlReader = com.ExecuteReader();

If you want to insert data, you probably don't want to use something with DataReader in its name. Use a DataAdapter instead.


A bigger problem

A bigger problem is that you seem to have copied and pasted code into your InsertInfo web method without reading it. If you'd read your code, your brain should have locked up on at least one of the six tokens that include either Read or Reader. And after locking up, your brain should have said to itself, "Self, where am I updating or inserting *anything? I seem to just be reading stuff."

Cultivate that skill by reading other people's code, then by reading your own code. In my experience, you won't learn very quickly just by reading your own code. Our brains seem to make our eyes blind to our own errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.