【问题标题】:Problem while inserting data from GUI layer to database将数据从 GUI 层插入数据库时​​出现问题
【发布时间】:2011-01-21 22:39:57
【问题描述】:

我在将新记录从 GUI 部分插入数据库表时遇到问题。我创建了带有 id、name、age 等的数据库表 Patient ......id 是身份主键。我的问题是,当我在表中插入重复名称时,控件应该转到其他部分,并显示如下消息...此名称已经存在,请尝试使用另一个名称... 但是在我的编码中没有得到.....这是所有代码...请有人指出我出了什么问题或怎么做??? GUILayer:

protected void BtnSubmit_Click(object sender, EventArgs e)
{
        if (!Page.IsValid)
        return;
        int intResult = 0;
        string name = TxtName.Text.Trim();
        int age = Convert.ToInt32(TxtAge.Text);
        string gender;
        if (RadioButtonMale.Checked)
        {
            gender = RadioButtonMale.Text;
        }
        else
        {
            gender = RadioButtonFemale.Text;
        }
        string city = DropDownListCity.SelectedItem.Value;

        string typeofdisease = "";
        foreach (ListItem li in CheckBoxListDisease.Items)
        {
            if (li.Selected)
            {
                typeofdisease += li.Value;
            }
        }
        typeofdisease = typeofdisease.TrimEnd();
        PatientBAL PB = new PatientBAL();
        PatientProperty obj = new PatientProperty();
        obj.Name = name;
        obj.Age = age;
        obj.Gender = gender;
        obj.City = city;
        obj.TypeOFDisease = typeofdisease;  
        try
        {
            intResult = PB.ADDPatient(obj);
            if (intResult > 0)
            {
                lblMessage.Text = "New record inserted successfully.";
                TxtName.Text = string.Empty;
                TxtAge.Text = string.Empty;
                RadioButtonMale.Enabled = false;
                RadioButtonFemale.Enabled = false;
                DropDownListCity.SelectedIndex = 0;
                CheckBoxListDisease.SelectedIndex = 0;                     
            }
            else
            {
                lblMessage.Text = "Name [<b>" + TxtName.Text + "</b>] alredy exists, try another name";


            }                
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
        }
        finally
        {
            obj = null;
            PB = null;
        }        

}

BAL 层:

public class PatientBAL
{
public int ADDPatient(PatientProperty obj)
{
    PatientDAL pdl = new PatientDAL();
    try
    {
        return pdl.InsertData(obj);
    }
    catch
    {
        throw;
    }
    finally
    {
        pdl=null;
    }
}

}

DAL 层:

public class PatientDAL
{
public string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;    
public int InsertData(PatientProperty obj)
{
    SqlConnection con = new SqlConnection(ConString);
    con.Open();
    SqlCommand com = new SqlCommand("LoadData",con);
    com.CommandType = CommandType.StoredProcedure;

    try
    {
        com.Parameters.AddWithValue("@Name", obj.Name);
        com.Parameters.AddWithValue("@Age",obj.Age);
        com.Parameters.AddWithValue("@Gender",obj.Gender);
        com.Parameters.AddWithValue("@City", obj.City);
        com.Parameters.AddWithValue("@TypeOfDisease", obj.TypeOFDisease);
        return com.ExecuteNonQuery();
    }
    catch
    {
        throw;
    }
    finally
    {
        com.Dispose();
        con.Close();
    }          
}

}

属性类:

public class PatientProperty
{
private string name;
private int age;
private string gender;
private string city;
private string typedisease;

public string Name
{
    get
    {
        return name;
    }
    set
    {
        name = value;
    }
}
public int Age
{
    get
    {
        return age;
    }
    set
    {
        age = value;
    }
}
public string Gender
{
    get
    {
        return gender;
    }
    set
    {
        gender = value;
    }
}
public string City
{
    get
    {
        return city;
    }
    set
    {
        city = value;
    }
}
public string TypeOFDisease
{
    get
    {
        return typedisease;
    }
    set
    {
        typedisease = value;
    }
}

}

这是我的存储过程: 创建过程加载数据 ( @Name varchar(50), @年龄整数, @性别字符(10), @City 字符(10), @TypeofDisease varchar(50) ) 作为 插入患者(姓名、年龄、性别、城市、疾病类型)值(@Name、@Age、@Gender、@City、@TypeofDisease) 去吧

【问题讨论】:

  • 您应该重新格式化最后一个清单中的代码。而且您没有提供数据库查询。如我所见,您需要从中返回一个负值才能显示“名称存在”警告。
  • 可以分享一下SQL建表SP吗?或者至少是它的结构。

标签: c# database


【解决方案1】:

您确定您的 LoadData 存储过程正在执行插入而不是更新吗?

【讨论】:

  • 嗨,这是我的存储过程:CREATE PROCEDURE LoadData (@Name varchar(50), @Age int, @Gender char(10), @City char(10), @TypeofDisease varchar(50) ) 插入患者(姓名、年龄、性别、城市、疾病类型)值(@Name、@Age、@Gender、@City、@TypeofDisease)GO
  • Rahul 使用编辑问题选项将上述代码添加到您的主要问题中。
【解决方案2】:

看起来你抛出了错误,所以它跳到你的 catch 块。

您需要处理从 PB.ADDPatient 返回的错误,而不是值

【讨论】:

    【解决方案3】:

    记录在数据库中是否重复?恐怕你没有为数据库中的Names添加唯一约束!

    如果是这种情况,并且您使用的是 SQL Server,请检查以下内容: SQL Server 2005 How Create a Unique Constraint?

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2013-08-27
      • 1970-01-01
      相关资源
      最近更新 更多