Solved

INSERT Data into SQL C#/.NET

Posted on 2010-09-23
2
432 Views
Last Modified: 2013-11-26
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
Comment
Question by:Shaggy1
2 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 125 total points
ID: 33749073
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
 

Author Closing Comment

by:Shaggy1
ID: 33749208
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 38
Using CTE to insert records into a table 2 28
Generate Weekly Schedule 15 17
Sql Query Datatype 2 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now