5 Replies - 383 Views - Last Post: 29 April 2014 - 07:29 AM Rate Topic: -----

#1 columbo1977  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-March 14

Problem with c#, SQL query

Posted 29 April 2014 - 03:44 AM

I have a program for editing a DB entry but when I run it I am getting errors about converting strings. Can anyone help?
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ICA
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool more = true;
                do
            {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine("Select an Option");
                    Console.WriteLine("--------------------------");
                    Console.WriteLine("1 Add New Staff");
                    Console.WriteLine("2 Edit Staff");
                    Console.WriteLine("3 Delete Staff");
                    Console.WriteLine("0 Exit");
                    Console.WriteLine("----------------------------");
                    ConsoleKeyInfo key = Console.ReadKey();
                    switch (key.KeyChar.ToString())
                    {
                        case "1":
                            InsertQuery(getUserInput(),getUserInput(),getUserInput());
                            break;
                        case "2":
                            EditQuery(getUserInput());
                            break;
                       // case "3":
                       //     DeleteQuery();
                      //      break;
                        default:
                            more = false;
                            break;
                    }
                }
                while (more);
            }
            private static void InsertQuery(string s, string se, string sl) 
            { 
                using (var context = new ICAEntities()) 
                { 
                    //create an object 
                    tblStaff staff = new tblStaff(); 
                    staff.StaffName = s;
                    staff.StaffEmail = se;
                    staff.StaffLocation = sl;
                    //add object to container's result entity set and save, catch exceptions 
                        try 
                        { 
                            context.tblStaffs.Add(staff); 
                            context.SaveChanges(); 
                            Console.WriteLine(" ... done"); 
                        } 
                        catch (Exception ex) 
                        { 
                            Console.WriteLine("error " + ex.Message); 
                        } 
                    } 
            }
    
    
            private static void EditQuery(int mc) 
            { 
                using (var context = new ICAEntities()) 
                { 
                    try 
                    {
                        //get the row containing the given primary key 
                        var staff = (from m in context.tblStaffs 
                                      where m.StaffID == mc 
                                      select m).First();
                        //get user input  see function above 
                        string newName = getUserInput(); 
                        //change module entity and save, catch exceptions 
                        staff.StaffName = newName; 
                        context.SaveChanges(); 
                        Console.WriteLine(" ... done"); 
                    } 
                    catch (Exception ex) 
                    { 
                        Console.WriteLine("error " + ex.Message); 
                    } 
                } 
            }
        
        private static void DoY()
            {
                // blah blah
            }
    
            private static string getUserInput() 
            { 
                Console.WriteLine(" enter input value and press return key"); 
                Console.Write("?"); 
                string inValue = Console.ReadLine(); 
                return inValue; 
            }
    
        }
    }



the errors I am getting are :

The best overloaded method match for "ICA.Program.EditQuery(int)' has some invalid arguments

and :

Argument 1: cannot convert from string to int

The GetuserInput works for adding something but when editing it isnt working as I am choosing a StaffID to identify the record that need editing and the StaffID is an INT.

Any ideas?

Thanks

Graham

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with c#, SQL query

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Problem with c#, SQL query

Posted 29 April 2014 - 05:54 AM

Yes, your function to edit takes an int, but your function to get input returns a string. Consider using TryParse() or Parse() on the string that you get back.
Was This Post Helpful? 0
  • +
  • -

#3 columbo1977  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-March 14

Re: Problem with c#, SQL query

Posted 29 April 2014 - 06:19 AM

I just got Parse or tryparse dont exist in current context?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Problem with c#, SQL query

Posted 29 April 2014 - 06:29 AM

Show us your new code.
Was This Post Helpful? 0
  • +
  • -

#5 columbo1977  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-March 14

Re: Problem with c#, SQL query

Posted 29 April 2014 - 07:23 AM

View PostSkydiver, on 29 April 2014 - 06:29 AM, said:

Show us your new code.

I got it working by copying and modifying the input code for an INT

        private static int getUserInputInt()
        {
            Console.WriteLine(" Enter Number");
            Console.Write("?");
            int inValue = Convert.ToInt32(Console.ReadLine());
            return inValue; 
        }



The entire code in case it helps anyone is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ICA
{
    class Program
    {
        static void Main(string[] args)
        {
            bool more = true;
            do
        {
                Console.WriteLine("----------------------------");
                Console.WriteLine("Select an Option");
                Console.WriteLine("--------------------------");
                Console.WriteLine("1 Add New Staff");
                Console.WriteLine("2 Edit Staff");
                Console.WriteLine("3 Delete Staff");
                Console.WriteLine("0 Exit");
                Console.WriteLine("----------------------------");
                ConsoleKeyInfo key = Console.ReadKey();
                switch (key.KeyChar.ToString())
                {
                    case "1":
                        InsertQuery(getUserInput(),getUserInput(),getUserInput());
                        break;
                    case "2":
                        EditQuery(getUserInputInt());
                        break;
                    case "3":
                        DeleteQuery(getUserInputInt());
                        break;
                    default:
                        more = false;
                        break;
                }
            }
            while (more);
        }
        private static void InsertQuery(string s, string se, string sl) 
        { 
            using (var context = new ICAEntities()) 
            { 
                //create an object 
                tblStaff staff = new tblStaff(); 
                staff.StaffName = s;
                staff.StaffEmail = se;
                staff.StaffLocation = sl;
                //add object to container's result entity set and save, catch exceptions 
                    try 
                    { 
                        context.tblStaffs.Add(staff); 
                        context.SaveChanges(); 
                        Console.WriteLine(" ... done"); 
                    } 
                    catch (Exception ex) 
                    { 
                        Console.WriteLine("error " + ex.Message); 
                    } 
                } 
        }


        private static void EditQuery(int mc) 
        { 
            using (var context = new ICAEntities()) 
            { 
                try 
                {
                    //get the row containing the given primary key 
                    var staff = (from m in context.tblStaffs 
                                  where m.StaffID == mc
                                  select m).First();
                    //get user input  see function above 
                    string newName = getUserInput(); 
                    //change module entity and save, catch exceptions 
                    staff.StaffName = newName; 
                    context.SaveChanges(); 
                    Console.WriteLine(" ... done"); 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine("error " + ex.Message); 
                } 
            } 
        }

        private static int tryParse(string mc)
        {
            throw new NotImplementedException();
        }
    
    private static void DeleteQuery(int mc) 
    {
        using (var context = new ICAEntities()) 
        { 
            try 
            { 
                //get the row containing the given primary key 
                var staff = (from m in context.tblStaffs 
                              where m.StaffID == mc 
                              select m).First();
                //delete object from container's module entity set and save, catch exceptions 
                context.tblStaffs.Remove(staff); 
                context.SaveChanges(); 
                Console.WriteLine(" ... done"); 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine("error " + ex.Message); 
            } 
        } 
    }

        private static string getUserInput() 
        { 
            Console.WriteLine(" enter input value and press return key"); 
            Console.Write("?"); 
            string inValue = Console.ReadLine(); 
            return inValue; 
        }
        private static int getUserInputInt()
        {
            Console.WriteLine(" Enter Number");
            Console.Write("?");
            int inValue = Convert.ToInt32(Console.ReadLine());
            return inValue; 
        }

    }
}




Cheers

Graham
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Problem with c#, SQL query

Posted 29 April 2014 - 07:29 AM

Did you follow Skydiver's link and read up on how to correctly use the Parse/TryParse methods? If not, read his link.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1