S
S
spone3792017-03-19 17:53:39
ASP.NET
spone379, 2017-03-19 17:53:39

Does the name exist in the database?

The function of checking whether the name exists in the database does not work. It doesn't throw any errors, it just writes the same names further. Tell me how to fix? Thanks in advance

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;
using System.Configuration;
using System.Threading.Tasks;


public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from reg where Name = '" + TextBoxName.Text + "'";
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp == 1) {
                Response.Write("User already exists!");
            }

            conn.Close();
        }

    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string str = "";
            for (int i = 0; i < CheckBoxListPrograms.Items.Count - 1; i++)
            {
                if (CheckBoxListPrograms.Items[i].Selected){
                    str += CheckBoxListPrograms.Items[i].Text.ToString() + ",";}
                
                
            }
            str = str.TrimEnd(',');

            Guid newGUID = Guid.NewGuid();


                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
                        conn.Open();
                        string insertQuery = "insert into reg (Name, Email, Gender, Country, Programs) values(@name, @email, @gender, @country, @programs)";
                        SqlCommand com = new SqlCommand(insertQuery, conn);
                        
                        com.Parameters.AddWithValue("@name", TextBoxName.Text);
                        com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
                        com.Parameters.AddWithValue("@gender", RadioButtonListGender.Text);
                        com.Parameters.AddWithValue("@country", DropDownListCountry.SelectedItem.ToString());
                        com.Parameters.AddWithValue("@programs", str);

                        com.ExecuteNonQuery();
                        Response.Redirect("Manager.aspx");
                        Response.Write("Done!)");

                        conn.Close();
        }
        catch(Exception ex) {
            Response.Write("Error" + ex.ToString());
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spone379, 2017-03-19
@spone379

The issue is resolved, it was necessary to put the name of the table in square brackets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question