Solved

Store Procedure

Posted on 2011-03-07
5
241 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
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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