12 Replies - 443 Views - Last Post: 14 August 2019 - 08:14 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 12:04 PM

Problem
Error out parameter must be assigned before control leave current method .

function GetInsertStatmentText below return full insert statement as below :
INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);


in out parameter i need to return

@id, @branch_id, @name, @address, @phone

when i add out parameter to function i get error


Error out parameter must be assigned before control leave current method

How to solve this error please ?

What I have tried:


public static string GetInsertStatmentText(string JsonData,out  sqp)
        {
            string Insert = "";
            JObject jo = JObject.Parse(JsonData);
            JToken m = jo["master"];
            string connectionstring = "Server=AHMEDSALAH-PC\\SQL2014;Database=Atum;User Id=sa;Password=abc123;"; //change connection string
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                using (SqlCommand command = new SqlCommand(Jsonhelper.GetInsertStatement(m), connection))
                {
                    connection.Open();
                    List<SqlParameter> lsp = Jsonhelper.GetSqlParams(jo["master"]);
                    foreach (SqlParameter sqp in lsp)
                        command.Parameters.Add(sqp);
                    Insert = command.CommandText;
                }
            }
            return Insert;

        }


Is This A Good Question/Topic? 0
  • +

Replies To: Error out parameter must be assigned before control leave current meth

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 12:08 PM

sqp has no defined type. Parameters desire types.
public static string GetInsertStatmentText(string JsonData,out  sqp)

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 12:14 PM

Why is sqp even being used as a parameter. Line 13 declares another sqp which will hide the one that is passed as a parameter.
Was This Post Helpful? 0
  • +
  • -

#4 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:05 PM

can you please modify function and tell me what i do
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:06 PM

You should know well enough that folk will not be doing your work for you.

You already have a parameter with a type. What's confusing about "all parameters must have types"?
Was This Post Helpful? 0
  • +
  • -

#6 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:12 PM

How to get parameters from function
parameters only with insert statement
are there are any way to do that please

by any way i need function return
1- INSERT INTO master_table(id, branch_id, name, address, phone) VALUES(@id, @branch_id, @name, @address, @phone);
[email protected], @branch_id, @name, @address, @phone
can any one help me
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:19 PM

ahmedba: You are confusing yourself between parameters for C# methods, and parameters for SQL commands. Your question in post #6 is with regards to SQL commands, but your original post question is talking about an error regarding C# methods.

Your lines 12-14 are already getting passing the SQL parameters to the SQL command by pulling the parameters extracted by Jsonhelper.GetSqlParams(jo["master"]). If you didn't know that you were already doing this, I highly suggest talking to the person who just gave you this code without explaining it to you, that it is better to teach a man to fish, than to give a man a fish.
Was This Post Helpful? 0
  • +
  • -

#8 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:27 PM

this is good i already do that but how to separate by comma ,
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 01:51 PM

SQL INSERT statements do not really return anything. I am not certain what you are expecting.

There are SQL parameter tutorials here. I suggest you look them over if you do not know how to do that.

All of this is an aside to the issue mentioned about the C#'s function and missing parameter data type.

Also asking for folk to do their work here:
https://social.msdn....m=csharpgeneral

https://www.c-sharpc...ontrol-leave-cu
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 02:17 PM

Let's try a different tack. Why do did you add out spq to this:
public static string GetInsertStatmentText(string JsonData,out  sqp)



If all you needed to do is to figure out what got passed in as SQL parameters, why not just use the code:
List<SqlParameter> lsp = Jsonhelper.GetSqlParams(jo["master"]);


Each SqlParameter in the list will have the name of the parameter and the value passed in.

As I said above, if you don't know what lines 12-14 were doing, then you need to talk to whoever gave you that code and have them explain how and why it works, because you obviously didn't write it yourself otherwise you would know what it does, and more importantly, that it already fulfills your need in terms of getting the values that you wanted.
Was This Post Helpful? 0
  • +
  • -

#11 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1445
  • View blog
  • Posts: 2,057
  • Joined: 24-August 11

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 07:43 PM

Can you tell us why your method is static?
Can you tell us what an out parameter does?
Can you tell us more about the Jsonhelper class? I know it's not in the Newtonsoft library that the rest of your JSON-handling code comes from.
It looks like Insert is a class-level property or field, since it's not declared within the method body. Can you tell us why you're assigning the output to Input and returning the value from the method? - thanks Skydiver, I must have skimmed past that first line.

I'm not saying these decisions aren't valid outcomes of a design session. I'm just asking if you know why they're in the code, so we can better understand if you even know what the code is supposed to be doing. I'll gladly apologize and help in any way that I can if you can answer those questions.

This post has been edited by h4nnib4l: 15 August 2019 - 04:43 AM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 08:04 PM

From what I could see, the Jsonhelper is shown in one of the links in post #9.

Insert is actually declared as a string on line #3 in the OP. It's just not obvious on first reading because of the OP's inconsistent naming -- sometimes he follows the .NET Framework naming convention, sometimes he doesn't. To me it's a hallmark of copy and paste code that's been cobbled together.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Error out parameter must be assigned before control leave current meth

Posted 14 August 2019 - 08:14 PM

Oh look, our OP has given up on using parameterized queries and gone on to building up strings again, and moved on to another related question in StackOverflow.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1