0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



namespace studentA
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void saveButton_Click(object sender, EventArgs e)
        {

            string name = nameTextBox.Text;
            string email = emailTextBox.Text;
            string phone = phoneTextBox.Text;
            string reg = regTextBox.Text;

            Student student = new Student(name, email, phone, reg);


            string connectionString = @"Server=.\SQLEXPRESS;Database=db;Integrated Security=true";
            SqlConnection connection = new SqlConnection(connectionString);

            string query = "INSERT INTO Students VALUES('" + student.Name + "','" + student.Email + "','" + student.Phone + "','" + student.Reg + "')";

            SqlCommand command = new SqlCommand(query, connection);

            connection.open();
            int rowAffected=command.ExecuteNonQuery();
            connection.close();

            if (rowAffected > 0)
            {
                messageLabel.Text = "success";
            }
            else
            {
                messageLabel.Text = "failed";
            }
            Response.Write(rowAffected);

        }
    }
}

The error is on SqlConnection connection = new SqlConnection(connectionString); that line. I am using MSVS 2013 and microsoft sql server. I searched on google and get solution like "This can be the result of a .Net framework version incompatibility between two projects." But I don't understand about it. I also try with adding System.Data.SqlClient;

joy oares
  • 155
  • 2
  • 10
  • 3
    The SqlConnection class belongs to the System.Data.SqlClient namespace. If you want to use it then you need to add a _using System.Data.SqlClient;_ to your list of used namespaces (or change your code to use the full name _System.Data.SqlClient.SqlConnection connection = ...._ – Steve Mar 30 '16 at 12:42
  • The error message says it all. You are missing a `using` directive. Simple matter of googling to find out which assembly `SqlConnection` is a member of. – Tab Alleman Mar 30 '16 at 12:47
  • when i added System.Data.SqlClient namespace, it gives another error: Error 1 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'close' and no extension method 'close' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?) C:\Users\joyoa\Desktop\mvc\studentA\studentA\WebForm1.aspx.cs 41 24 studentA – joy oares Mar 30 '16 at 13:04
  • yes. Exactly. That is. – joy oares Mar 30 '16 at 13:43
  • I solve my problem writing the code connection.Open(); int rowAffected = command.ExecuteNonQuery(); connection.Close(); INSTEAD OF connection.open(); int rowAffected = command.ExecuteNonQuery(); connection.close(); I used lowercase :( Thanks everybody for helping me. – joy oares Mar 30 '16 at 19:10

0 Answers0