spone379
@spone379

Существует ли имя в БД?

Не работает функция проверки на то, существует ли имя в БД. Ошибок не выдает, просто записывает одинаковые имена дальше. Подскажите как исправить? Заранее спасибо

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());
        }
    }
}
  • Вопрос задан
  • 169 просмотров
Решения вопроса 1
spone379
@spone379 Автор вопроса
Вопрос решен, нужно было название таблицы взять в квадратные скобки
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы