Link to home
Start Free TrialLog in
Avatar of ferguson_jerald
ferguson_jerald

asked on

Need help with inserting data into a sql server database.

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
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

> 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.
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.
Avatar of ferguson_jerald
ferguson_jerald

ASKER

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?
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Not a problem, glad to help.