Stored procedure to update multiple table same time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parshupooja
    New Member
    • Jun 2007
    • 159

    Stored procedure to update multiple table same time

    Hello ,

    I am using asp.net C# SQL Server . I have form on asp.net page consists various fields.
    field 1, field 2 ------ upto field 10
    on click of submit button i need to save all information entered by user into table1, table2, table3 tables.
    field 1 to field5 is new record in table1 whereas field 6, field 7 , field 8 will update some columns in table 2 and rest fields will be updated to table 3.

    To acheive this I need to write Stored procedure which saves this information to tables. Please help
  • bhar
    Banned
    New Member
    • Apr 2006
    • 37

    #2
    Hi,

    After you enter the data into the fields, user will press the button control. The code is based on C# 2005. You need to pass the values from the page as parameters to the stored procedure.
    [code=vb]
    void Button_Click(ob ject sender, System.EventArg s e)
    {

    SqlConnection conJobs;
    string strConString;
    SqlCommand cmdInsert;
    SqlParameter parmReturn;

    if (IsValid)
    {
    strConString = "DataSource=loc alhost;database =jobsabc;User ID=jobs123;Pass word=cd546dc;";

    conJobs = new SqlConnection(s trConString);
    cmdInsert = new SqlCommand("add User", conJobs);
    cmdInsert.Comma ndType = CommandType.Sto redProcedure;
    parmReturn = cmdInsert.Param eters.Add("@ret urn", SqlDbType.Int);
    parmReturn.Dire ction = ParameterDirect ion.ReturnValue ;

    cmdInsert.Param eters.Add("@use rname", txtUsername.Tex t);
    cmdInsert.Param eters.Add("@pas sword", txtPassword.Tex t);
    cmdInsert.Param eters.Add("@fir stname", txtFirstname.Te xt);
    cmdInsert.Param eters.Add("@las tname", txtLastname.Tex t);


    conJobs.Open();
    cmdInsert.Execu teNonQuery();
    conJobs.Close() ;

    if ((int)cmdInsert .Parameters["@Return"].Value == 0)
    {

    Response.Redire ct("registeracc ept.aspx");
    }

    if ((int)cmdInsert .Parameters["@Return"].Value == -1)
    {
    lblError.Visibl e = true;
    }
    }
    }

    The code for the stored procedure is given below.

    CREATE procedure addUser
    (
    @username Varchar( 20 ),
    @password varchar( 20 ),
    @firstname varchar( 30 ),
    @lastname varchar( 30 )
    )
    As

    If Exists( SELECT ul_username FROM UserList
    WHERE ul_username = @username )
    BEGIN
    RETURN -1
    END

    If Exists( SELECT ul_email FROM UserList
    WHERE ul_email = @email )
    BEGIN
    RETURN -2
    END

    ELSE
    INSERT UserList (
    ul_username,
    ul_password,
    ul_firstname,
    ul_lastname

    ) VALUES (
    @username,
    @password,
    @firstname,
    @lastname
    )[/code]
    Last edited by pbmods; Oct 22 '07, 11:59 AM. Reason: Added CODE tags, removed commercial link.

    Comment

    • parshupooja
      New Member
      • Jun 2007
      • 159

      #3
      Hello bhar,

      Thank You for reply but please read my doubt again.
      I am trying to insert and update multiplte tables simultaneouly in single stored procedure

      Comment

      Working...