Solved

Store Procedure

Posted on 2011-03-07
5
243 Views
Last Modified: 2012-05-11
Hi ,

am using inline insert query in my code.

  cmd.CommandText = "INSERT Person (EmployeeNo,NamePrefix,FirstName, MiddleName, LastName, NameSuffix,Contractor,HighestEducationalLevel, BirthDate, Gender ) VALUES("
                                                   + item.EmployeeNumber + ","
                                                   + "'" + item.Title + "'" + ","
                                                   + "'" + item.PreferredName + "'" + ","
                    //+ "'" + item.MiddleName + "'" + ","
                                                   + "'" + item.MiddleName.Replace("'", "''") + "'" + ","
                                                    + "'" + item.Surname.Replace("'", "''") + "'" + ","
                                                    + "'" + item.Suffix + "'" + ","
                                                    + "'" + item.PayPoint + "'" + ","
                                                    + "'" + item.HighestEducationLevel + "'" + ","
                                                    + "'" + item.BirthDate + "'" + ","
                                                    + "'" + item.Sex + "'" + ")";

                cmd1.CommandText = "INSERT HOMEADDRESS (EmployeeNo,AddressLine1,AddressLine2,AddressLine3,City,State,PostCode ) VALUES("
                                                   + item.EmployeeNumber + ","
                                                    + "'" + item.ResidentialAddress1+ "'" + ","
                                                    + "'" + item.ResidentialAddress2 + "'" + ","
                                                    + "'" + item.ResidentialAddress3 + "'" + ","
                                                    + "'" + item.ResidentialAddress2 + "'" + ","
                                                    + "'" + item.ResidentialAddress3 + "'" + ","
                                                    + "'" + item.ResidentialPostCode + "'" + ")";

Open in new window


Any one give me an idea how can i combine both and write it in one Store procedure in that case i can clean up some mess in my code.

Thanks in Advance!
0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 35062642
You can use semicolumn to separate the 2 calls.

...
+ "'" + item.Sex + "'" + ");"
+ "INSERT HOMEADDRESS (EmployeeNo,AddressLine1,AddressLine2,AddressLine3,City,State,PostCode ) VALUES("
+ ...

0
 

Author Comment

by:Sha1395
ID: 35062884
Thanks for your comment BurnieP.The reason i want to do in SP is

This my actual Insert command

               jobCmd.CommandText = "INSERT HOMEADDRESS (EmployeeNo,StateCode) VALUES("
                                                     + item.EmployeeNumber + ","
                                                      + "'" + item.CityLocation+ "'" + ")";


Am pulling value for City Location to State Code.

For Example

if the city location is Detroit then am passing the value "MI" to State code in DB.

How can i achieve,i believe its easy thru SP.
0
 
LVL 11

Accepted Solution

by:
lenordiste earned 500 total points
ID: 35063221
in short you could write this stored procedure:
CREATE PROCEDURE InsertPersonalInformation
    @EmployeeNo varchar(50), 
    @NamePrefix varchar(50) ,
    @StateCode varchar(50) 
AS 

    SET NOCOUNT ON;
  INSERT Person (@EmployeeNo,@NamePrefix)
  INSERT HOMEADDRESS (@EmployeeNo,@StateCode)
GO

Open in new window


you would then call your stored procedure like so:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmployeeNo", item.EmployeeNumber));
cmd.Parameters.Add(new SqlParameter("@NamePrefix", item.NamePrefix));
cmd.Parameters.Add(new SqlParameter("@StateCode", item.StateCode));
cmd.ExecuteNonQuery();

Open in new window

0
 
LVL 11

Expert Comment

by:lenordiste
ID: 35063241
oh and don't forget to specify the stored procedures name in the constructor of SqlCommand:
SqlCommand cmd  = new SqlCommand(      "InsertPersonalInformation", conn);
0
 

Author Comment

by:Sha1395
ID: 35063362
Hi lenordiste,

Thanks for your SP can you help to replace the string inside SP.

For example

am getting city location ="Detroit"

then for State code ="MI" ( i have to insert this value to my State Code Column)

how can i write "Case"Statetment or if condition inside SP ?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question