I am trying to insert into a DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rebel123
    New Member
    • Feb 2010
    • 1

    I am trying to insert into a DB

    I am trying to insert into a SQL DB but it is not working...I ran the query in SQL management studio which was the following:

    Code:
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Cycle"].ConnectionString);
                        SqlCommand comm = new SqlCommand("INSERT into [CyclistDetails] (Surname, FirstName, PostalAddress, MobileNumber, EmailAddress, EmailConfirmed, NextOfKin, AccLimerick, AccGalway, TransportToCork, BikeTransportToDublin, BikeTransportToCork, ShirtSize, DietRequirements, AttendingBBQ, ParticipatedBefore, HearOfEvent, LevelOfCycling, TermsAndConditions) VALUES (@c_Surname,@c_FirstName,@c_PostalAddress,@c_MobileNumber,@c_EmailAddress,@c_EmailAddressConfirmed,@c_NextOfKin,@c_AccLimerick,@c_AccGalway,@c_TransportToCork,@c_BikeTransportToDublin,@c_BikeTransportToCork,@c_ShirtSize,@c_DietRequirements,@c_AttendingBBQ,@c_ParticipatedBefore,@c_HearOfEvent,@c_LevelOfCycling,@c_TermsAndConditions)", conn);
    
                        comm.Parameters.Add("@c_Surname", SqlDbType.NVarChar).Value = txtSname.Text;
                        comm.Parameters.Add("@c_FirstName", SqlDbType.NVarChar).Value = txtFname.Text;
    
                        string address;
                        address = txtAddress1.Text + "," + txtAddress2.Text + "," + txtAddress3.Text + "," + txtAddress4.Text;
    
                        comm.Parameters.Add("@c_PostalAddress", SqlDbType.NVarChar).Value = address.ToString();
                        comm.Parameters.Add("@c_MobileNumber", SqlDbType.Int).Value = Convert.ToInt32(txtMobile.Text);
                        comm.Parameters.Add("@c_EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text;
                        comm.Parameters.Add("@c_EmailAddressConfirmed", SqlDbType.NVarChar).Value = txtEmail2.Text;
                        comm.Parameters.Add("@c_NextOfKin", SqlDbType.Int).Value = Convert.ToInt32(txtNextOfKin.Text);
                        comm.Parameters.Add("@c_AccLimerick", SqlDbType.NVarChar).Value = ddlAccLimerick.Text;
                        comm.Parameters.Add("@c_AccGalway", SqlDbType.NVarChar).Value = ddlAccGalway.Text;
                        comm.Parameters.Add("@c_TransportToCork", SqlDbType.NVarChar).Value = ddlTransportCork.Text;
                        comm.Parameters.Add("@c_BikeTransportToDublin", SqlDbType.NVarChar).Value = ddlBikeTransportToDublin.Text;
                        comm.Parameters.Add("@c_BikeTransportToCork", SqlDbType.NVarChar).Value = ddlBikeTransportCork.Text;
                        comm.Parameters.Add("@c_ShirtSize", SqlDbType.NVarChar).Value = ddlShirtSize.Text;
                        comm.Parameters.Add("@c_DietRequirements", SqlDbType.NVarChar).Value = ddlDiet.Text;
                        comm.Parameters.Add("@c_AttendingBBQ", SqlDbType.NVarChar).Value = ddlBBQ.Text;
                        comm.Parameters.Add("@c_ParticipatedBefore", SqlDbType.NVarChar).Value = ddlParticipatedBefore.Text;
                        comm.Parameters.Add("@c_HearOfEvent", SqlDbType.NVarChar).Value = ddlHearOfEvent.Text;
                        comm.Parameters.Add("@c_LevelOfCycling", SqlDbType.NVarChar).Value = ddlLevelOfCycling.Text;
                        comm.Parameters.Add("@c_TermsAndConditions", SqlDbType.NVarChar).Value = CheckBox1.Text;
    
    
    
                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close();

    And i got the following errors and my Data is not going into the DB:

    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near '='.
    Msg 103, Level 15, State 4, Line 2
    The identifier that starts with 'INSERT into [CyclistDetails] (Surname, FirstName, PostalAddress, MobileNumber, EmailAddress, EmailConfirmed, NextOfKin, AccLimer' is too long. Maximum length is 128.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Database How-to parts 1 and 2
    Database tutorial Part 1
    Database tutorial Part 2

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      I'm not a SQL expert, but this problem has something to do with quoted identifiers.

      Try making a stored procedure for your Insert and calling that instead of a raw sql statement.

      Comment

      Working...