samedi 23 avril 2016

C# INSERT INTO isn`t working (web sql)

I looked in the forum a lot and researched for my problem but I couldn`t find something that helped me. I have tried a lot of suggestions from here. I have a site and 3 sign up pages. every page have some information, validators are already set up and I built an SQL Server (Microsoft visual studio community 2015). Here is the code of the first sign up page:

 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;

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

    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        string str = "Data Source=(LocalDB)\\MSSQLLocalDB;";
        str += "AttachDbFilename=|DataDirectory|Database.mdf;";
        str += "Integrated Security= True";
        string email = null, userName = null;
        //Page
        if (Page.IsValid == true)
        {
            Response.Redirect("SignUp(2).aspx", true);
            email = txtEmail.Text;
            userName = txtUserName.Text;
        }
        //Email
        if (rfvEmail.IsValid == false || revEmail.IsValid == false)
        { txtEmail.CssClass = "txtError"; }
        else
        { txtEmail.CssClass = "Text"; }
        //User Name
        if (rfvUserName.IsValid == false || revUserName.IsValid == false)
        { txtUserName.CssClass = "txtError"; }
        else
        { txtUserName.CssClass = "Text"; }

        if (email != null && userName != null)//if there are values
        {
            using (SqlConnection c = new SqlConnection(str))
            {
                using (SqlCommand sqlCommand = new SqlCommand("INSERT INTO Table (Email, UserName) VALUES (@email, @userName);", c))
                {
                    sqlCommand.Connection = c;
                    sqlCommand.Parameters[@email].Value = email;
                    sqlCommand.Parameters[@userName].Value = userName;
                    c.Open();
                    sqlCommand.ExecuteNonQuery();
                    c.Close();
                }
            }
        }
    }

the INSERT isn`t inserting info to the table named Table in the database named Database.mdf.




Aucun commentaire:

Enregistrer un commentaire