Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Inserting data into another table

    Hello everyone,

    I've been following a book called 'ASP.NET 4 website using C# and VB'. How ever I have come across a problem I am trying to overcome. In chapter 9 of the book a page is created where you can insert data into a form and that data is then inserted into the SQL table. Now everything worked fine as I was following the tutorial. But then I decided to play around a bit and change the data so that instead of it showing the ID's of each table:


    it would show the what the actual category, subject and status is.


    this is what my .cs code looks like for the submit button when it is stored into the table and I'm sure there is something here I'm supposed to change or add?.
    I really don't know and I have spent all day trying to figure it out. Any help will be greatly appreciated!

    PHP Code:
    protected void submitButton_Click(object senderEventArgs e)
        {
            if (
    Page.IsValid)
            {
                
    // Define data objects
                
    SqlConnection conn;
                
    SqlCommand comm;
                
    // Read the connection string from Web.config
                
    string connectionString =
                    
    ConfigurationManager.ConnectionStrings[
                    
    "SPCC HelpDesk"].ConnectionString;
                
    // Initialize connection
                
    conn = new SqlConnection(connectionString);
                
    // Create command 
                
    comm = new SqlCommand("InsertHelpDesk"conn);
                
    // Specify we're calling a stored procedure
                
    comm.CommandType System.Data.CommandType.StoredProcedure;
                
    // Add command parameters
                
    comm.Parameters.Add("@EmployeeID"System.Data.SqlDbType.Int);
                
    comm.Parameters["@EmployeeID"].Value 5;

                
    comm.Parameters.Add("@StationNumber"System.Data.SqlDbType.Int);
                
    comm.Parameters["@StationNumber"].Value stationTextBox.Text;

                
    comm.Parameters.Add("@CategoryID"System.Data.SqlDbType.Int);
                
    comm.Parameters["@CategoryID"].Value categoryList.SelectedItem.Value;

                
    comm.Parameters.Add("@SubjectID"System.Data.SqlDbType.Int);
                
    comm.Parameters["@SubjectID"].Value subjectList.SelectedItem.Value;

                
    comm.Parameters.Add("@Description"System.Data.SqlDbType.NVarChar50);
                
    comm.Parameters["@Description"].Value descriptionTextBox.Text;

                
    comm.Parameters.Add("@StatusID"System.Data.SqlDbType.Int);
                
    comm.Parameters["@StatusID"].Value 1;
                
    // Enclose database code in Try-Catch-Finally
                
    try
                {
                    
    // Open the connection
                    
    conn.Open();
                    
    // Execute the command
                    
    comm.ExecuteNonQuery();
                    
    // Reload page if the query executed successfully
                    
    Response.Redirect("HelpDesk.aspx");
                }
                catch
                {
                    
    // Display error message
                    
    dbErrorMessage.Text =
                        
    "Error submitting the help desk request! Please " +
                        
    "try again later, and/or change the entered data!";
                }
                
    finally
                
    {
                    
    // Close the connection
                    
    conn.Close();
                }
            }
        }

    My stored procedure
    PHP Code:
    CREATE PROCEDURE InsertHelpDesk
    (
      @
    EmployeeID int,
      @
    StationNumber int,
      @
    CategoryID int,
      @
    SubjectID int,
      @
    Description nvarchar(50),
      @
    StatusID int
    )
    AS
    INSERT INTO HelpDesk (EmployeeIDStationNumberCategoryID,
        
    SubjectIDDescriptionStatusID)
    VALUES (@EmployeeID, @StationNumber, @CategoryID, @SubjectID,
        @
    Description, @StatusID
    Last edited by Ozzy; 04-09-2013 at 07:17 PM.

  • #2
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    you want actual category, subject and status to be inserted but in your code you have not added those fields.

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I totally agree with sunny68, as there is no such fields in the table that you want to insert.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •