Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with inserting data into a sql server database.

Posted on 2015-01-08
7
Medium Priority
?
96 Views
Last Modified: 2015-01-12
Hello Experts,

I need some assistance with a bit of code that's not working for me.  I am attempting to insert the contents of a text box into a table.  Easy enough, but for some reason I can't get it to work.  I am not getting an error message.  When I click the submit button nothing happens.  Any help would be greatly appreciated.  

I am using Visual Studio 2012, SqlServer 2008, asp.net web forms, and c#.

Here is the c# code I am using:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace saef_web_app
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source = TESTMEPRDSQL\\TESTME;Initial Catalog=saef;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {      
        }
        
        public void peeps()
        {
            string firstName = txt_emp_fn.Text;
        }
        protected void newEmpBtn_Click(object sender, EventArgs e)
        {
            SqlCommand insertcmd = new SqlCommand("INSERT INTO tbl_emp(emp_fn) VALUES(txt_emp_fn.Text)",conn);
            
            try
            {
                conn.Open();
                insertcmd.ExecuteNonQuery();
                Literal1.Text = "Data inserted successfully";
                conn.Close();
                peeps();
            }
            catch (Exception ex)
            {
                Literal1.Text = ex.Message;
            }
        }
    }
}

Open in new window


Thanks,
J
0
Comment
Question by:ferguson_jerald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40538955
> SqlCommand insertcmd = new SqlCommand("INSERT INTO tbl_emp(emp_fn) VALUES(txt_emp_fn.Text)",conn);
For starters, txt_emp_fn.Text is INSIDE the double-quotes, so the above statement would insert that exact text, and not the contents of txt_emp_fn.   I'm guessing you mean something like this...

 SqlCommand insertcmd = new SqlCommand("INSERT INTO tbl_emp(emp_fn) VALUES(" & txt_emp_fn.Text & ")",conn);

Disclaimer:  I know SQL well, but what I know about C# can be written on the back of a matchbook cover, in very large letters, using a grease pencil.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40538962
Change this line in the code

insertcmd.ExecuteNonQuery();

To this :

Dim effected As Integer = insertcmd.ExecuteNonQuery();

Then tell me what the value of effected is after the line executes.
0
 

Author Comment

by:ferguson_jerald
ID: 40539010
Thanks for the quick responses.  

Jim, your suggestion didn't work for the code.  Instead I get the error message that "Operator '&' cannot be applied to operands of type 'string' and 'string'.

Fernando, I changed insertcmd.ExecuteNonQuery(); to Dim effected As Integer = insertcmd.ExecuteNonQuery(); and got the following errors:

'The type or namespace name 'Dim' could not be found (are you missing a using directive or an assembly reference?)'
AND
'The type or namespace 'As' could not be found (are you missing a using directive or an assembly reference?)'

Thanks for the help thus far.  Do you have any other suggestions?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 40539152
Sorry, I don't know what I was thinking I gave you the code in VB not C#. Change  to this

int effected = insertcmd.ExecuteNonQuery();
0
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 1000 total points
ID: 40539725
Jim's also gave you a VB solution instead of C#. In C# you need to use the '+' sign to concatenate strings:
 SqlCommand insertcmd = new SqlCommand("INSERT INTO tbl_emp (emp_fn) VALUES('" + txt_emp_fn.Text + "')",conn);

Open in new window

NOTE: I also add single quote because you'll need it for text fields.
0
 

Author Comment

by:ferguson_jerald
ID: 40544419
Thanks for the feedback.  This seemed to be a tough one to learn with, but it makes sense now.  I need to pay attention to the syntax.  I was able to get it working by implementing your fixes along with a slight change in the code.  Here's what I got to work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace saef_web_app
{
    public partial class insertTblEmp : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source = TESTMEPRDSQL\\TESTME;Initial Catalog=saef;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void newEmpBtn_Click(object sender, EventArgs e)
        {
            SqlCommand insertcmd = new SqlCommand("INSERT INTO tbl_emp (emp_fn, emp_ln, emp_add, emp_city, emp_state, emp_zip) VALUES('" + txt_emp_fn.Text + "," + txt_emp_ln.Text + ", " + txt_emp_add.Text +", " + txt_emp_city.Text + ", " + txt_emp_state + ", " + txt_emp_zip.Text + "',)", conn);

            try
            {
                conn.Open();
                insertcmd.ExecuteNonQuery();
                Literal1.Text = "Data inserted successfully";
                conn.Close();
            }
            catch (Exception ex)
            {
                Literal1.Text = ex.Message;
            }
        }
    }
}

Open in new window


As you can see I added a couple more fields, and it works great.  Thanks for all of your assistance.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40544429
Not a problem, glad to help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 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