• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

INSERT Data into SQL C#/.NET

Unable to update a SQL table with form data....here is what i got. Thought maybe another set of eyes would help me out here.

Thanks!
protected void SubmitButton_Click(object sender, EventArgs e)
    {
        SqlConnection conn;
        SqlCommand comm;

        
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(
            "INSERT INTO contactForm(mailID,firstName,lastName,company,title,email,inquiry,interests,dateCreated) VALUES (@mailID,@firstName,@lastName,@company,@title,@email,@inquiry,@interests,@dateCreated)", conn);

        comm.Parameters.Add("mailID", SqlDbType.Int);
        comm.Parameters["mailID"].Value = hiddenID.Text;

        comm.Parameters.Add("@firstName", System.Data.SqlDbType.VarChar, 50);
        comm.Parameters["@firstName"].Value = txtFirstName.Text;

        comm.Parameters.Add("@lastName", System.Data.SqlDbType.VarChar, 50);
        comm.Parameters["@lastName"].Value = txtLastName.Text;

        comm.Parameters.Add("@company", System.Data.SqlDbType.VarChar, 50);
        comm.Parameters["@company"].Value = txtCompanyName.Text;

        comm.Parameters.Add("@title", System.Data.SqlDbType.VarChar, 50);
        comm.Parameters["@title"].Value = txtTitle.Text;

        comm.Parameters.Add("@email", System.Data.SqlDbType.VarChar, 70);
        comm.Parameters["@email"].Value = txtEmail.Text;   

        comm.Parameters.Add("@inquiry", System.Data.SqlDbType.VarChar, 70);
        comm.Parameters["@inquiry"].Value = txtInquiry.Text; 

        comm.Parameters.Add("@interests", System.Data.SqlDbType.VarChar, 200);
        comm.Parameters["@interests"].Value = txtInterests.Text;

        comm.Parameters.Add("@dateCreated", System.Data.SqlDbType.DateTime);
        comm.Parameters["@dateCreated"].Value = txtHiddenDate.Text; 

        try

        {
            conn.Open();
            comm.ExecuteNonQuery();
           
           
        }
        catch
        {
            this.MessageLabel.Text = "There was an error updating the selected page content!<br />";
            
        }
        finally
        {
            conn.Close();
        }
        
        
    }

Open in new window

0
Shaggy1
Asked:
Shaggy1
1 Solution
 
guru_samiCommented:
1: looks you need @maildId here:
comm.Parameters.Add("mailID", SqlDbType.Int);
comm.Parameters["mailID"].Value = hiddenID.Text;

2nd change your catch block to catch real exception like:

catch(Exception ex)
        {
            this.MessageLabel.Text = "There was an error updating the selected page content!<br />" + ex.Message;
           
        }



0
 
Shaggy1Author Commented:
Thanks Sami,

Can't believe I missed the @ for mailID..thanks for pointing that out.

Also, adding the real exception enabled me to find the problem..

the parameter for @mailID is a Primary Key which is incremental
and @dateCreated is auto populated with the getnow() value.

I removed those, and success!

Thanks for that extra set of eyes..!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now