Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert data into my database with two different tables. I have an error "A local variable named 'insCmd' is already defined in this scope. I under stand this part. How can I have the data to be inserted into one table and the other data inserted into another table?

C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;

public partial class FTEEnrollmentInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();


        string insCmd = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC)";

        string insCmd = "Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);

        insertUser.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
        insertUser.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
        insertUser.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
        insertUser.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        insertUser.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
        insertUser.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
        insertUser.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
        insertUser.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
        insertUser.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
        insertUser.Parameters.AddWithValue("@FCPTBHC", TextBoxTNCPG.Text);
        insertUser.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);
        insertUser.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
        insertUser.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
        insertUser.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
        insertUser.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
        insertUser.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
        insertUser.Parameters.AddWithValue("@BTRANSDERS", TextBoxTTOG.Text);
        insertUser.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
        insertUser.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("FinancialProfileFormA.aspx");
        }
        catch (Exception er)
        {
            Response.Write("You Have Successfully Submitted the Information!!!");
        }
        finally
        {

        }
    }
   public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        
    }
   protected void ButtonPrint_Click(object sender, EventArgs e)
   {
       
   }
}
Posted
Comments
joshrduncan2012 8-Jul-13 11:35am    
You need to call the insert strings two different variable names.

Also, the parameter declarations must reference which command they go with. If you want to use both insert statements, you need to have 2 separate insert commands with the corresponding parameters.
Computer Wiz99 8-Jul-13 11:47am    
Ok. Can you show me how that will look?
joshrduncan2012 8-Jul-13 11:51am    
First, differentiate between the 2 insCmd strings. Other than that, those look good.

You need one command per string.

SqlCommand insertCommand1 = new SqlCommand(insCmd1, conn);
SqlCommand insertCommand2 = new SqlCommand(insCmd2, conn);

The parameter declarations must have the corresponding command before the ".parameters.addwithvalue" calls. Other than that, they look good to me.
Computer Wiz99 8-Jul-13 11:55am    
Ok Thanks.
Computer Wiz99 8-Jul-13 12:04pm    
Ok, This is what I did:

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
con.Open();



string insCmd1 = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC)";

string insCmd2 = "Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE)";

SqlCommand insertUser = new SqlCommand(insCmd1, con);
insertUser.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
insertUser.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
insertUser.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
insertUser.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
insertUser.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
insertUser.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
insertUser.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
insertUser.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
insertUser.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
insertUser.Parameters.AddWithValue("@FCPTBHC", TextBoxTNCPG.Text);
insertUser.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);

SqlCommand insertCommand2 = new SqlCommand(insCmd2, con);
insertUser.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
insertUser.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
insertUser.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
insertUser.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
insertUser.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
insertUser.Parameters.AddWithValue("@BTRANSDERS", TextBoxTTOG.Text);
insertUser.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
insertUser.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);

Does this look right so far?

1 solution

change below line
C#
string insCmd = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC)";

        string insCmd = "Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE)";


to

string insCmd = "Insert into TableFTE (FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTBHC, NCHC) values (@FT_UNDERGR, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTBHC, @NCHC); Insert into TableGRADRATE (ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSDERS, YEAR, DATE) values (@ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSDERS, @YEAR, @DATE);";


and let me know whether this works or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900