如何在c#中解决这些问题

时间:2010-07-03 09:51:10

标签: c# exception data-access

  

可能重复:
  How to show a error message if the data is already inserted in the database in c#

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection("Data Source=KING;Initial Catalog=semasinter;Integrated Security=True");
        SqlCommand command = new SqlCommand("insert into StudentApplication", conn);
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "spinsertstudentapplication";
        command.Parameters.AddWithValue("@Student_id", TextBox1.Text);/// in these student id is typed and inserted its a primary key
        //command.Parameters.AddWithValue ("@Select_Course", ddlCourse.SelectedValue);
        command.Parameters.AddWithValue("@Select_Country", TextBox4.Text);
        command.Parameters.AddWithValue("@Select_State", TextBox2.Text);
        command.Parameters.AddWithValue("@Select_Franchise", TextBox3.Text);
        command.Parameters.AddWithValue("@Select_Sensei", ddlSensei.SelectedValue);
        command.Parameters.AddWithValue("@Group", TextBox6.Text);
        command.Parameters.AddWithValue("@Enter_Student_Name", TextBox7.Text);
        command.Parameters.AddWithValue("@Enter_Student_Address", TextBox8.Text);
        command.Parameters.AddWithValue("@Students_Father_Name", TextBox9.Text);
        command.Parameters.AddWithValue("@Student_DOB", TextBox10.Text);
        if (RadioButton2.Checked == true)
        {
            command.Parameters.Add("@Gender", SqlDbType.Bit).Value = true;

        }
        else
        {
            command.Parameters.Add("@Gender", SqlDbType.Bit).Value = false;

        }
        command.Parameters.Add("@Enter_Kyu", SqlDbType.NVarChar).Value = TextBox11.Text;
        conn.Open();
        int rows = command.ExecuteNonQuery();
        conn.Close();
    }
    catch(Exception ex)
    {

        //.Show(ex.Message);
    }

我想显示一条错误消息'已经插入了学生ID'我的朋友们我不知道怎么弄它bcz学生id id主键

0 个答案:

没有答案