0

It shows no error until and unless i add a record, it goes into catching exception which gives me connection problem as output. I tried alot but couldn't find my mistake.This is my code:

public partial class Default2 : System.Web.UI.Page
{
private string conStr = WebConfigurationManager.ConnectionStrings["StudentConnectionString1"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        FillList();
        BtnsActive(false, true, false, false, false, false);

    }
}

protected void BtnsActive(bool a, bool b, bool c, bool d, bool e, bool f)
{
    Panel2.Enabled = a;
    btnAdd.Enabled = b;
    btnInsert.Enabled = c;
    btnEdit.Enabled = d;
    btnUpdate.Enabled = e;
    btnDelete.Enabled = f;

}
protected void FillList()
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Select * from Student order by StudId", con);
    SqlDataReader reader;
    DropDownList1.Items.Clear();
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = reader["StudId"] + "," + reader["StudFirstName"];
            newItem.Value = reader["StudId"].ToString();
            DropDownList1.Items.Add(newItem);
        }
    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();

    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Select * from Student where StudId='" +  DropDownList1.SelectedValue + "'", con);
    SqlDataReader reader;

    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();

        TextBox1.Text = reader["StudId"].ToString();
        TextBox2.Text = reader["StudFirstName"].ToString();
        CheckBox1.Checked = (bool)reader["Library"];
    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, true, false, true);
    }
}
protected void btnAdd_Click(object sender, EventArgs e)
{
    BtnsActive(true, false, true, false, false, false);
    TextBox1.Text = "";

    TextBox2.Text = "";
    CheckBox1.Checked = false;
}
protected void btnInsert_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Insert into Student (StudId,StudFirstName,Library) values(@sid,@name,@library)", con);
    cmd.Parameters.AddWithValue("@sid", TextBox1.Text);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@library", CheckBox1.Checked);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('Record has been added.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }
}
protected void btnEdit_Click(object sender, EventArgs e)
{
    BtnsActive(true, false, false, false, true, false);
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Update Student set StudId=@id,StudFirstName=@name,Library=@library) where StudId=@oldId", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@library", CheckBox1.Checked);
    cmd.Parameters.AddWithValue("@oldId", DropDownList1.SelectedValue);
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('The Record has been Updated.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }
}
protected void btnDelete_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Delete from Student where StudId=@id", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('The Record has been Deleted.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }

}
}

and this is my design code which is simple..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"     Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:Panel ID="Panel1" runat="server" BackColor="#CC3300">
        Stud ID:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" />
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
        <asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
        <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />
        <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" BackColor="#FF9933">
        <br />
        <br />
        Stud ID:
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
        <br />
        Name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Library :
        <asp:CheckBox ID="CheckBox1" runat="server" />
    </asp:Panel>

</div>
</form>
</body>
</html>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Debug your code with the debugger. Firstly, your `catch` is saying `Connection problem`.. when in fact it could be many many things thrown from the query being sent. Print the exception message instead.. this will give you a better idea of the actual problem. – Simon Whitehead Apr 08 '14 at 03:28
  • which exception should i try catching ?? – user3447076 Apr 08 '14 at 03:59
  • Keep what you have.. but instead of printing `Connection problem` .. you should print `er.Message`. – Simon Whitehead Apr 08 '14 at 04:00
  • 1
    I think Simon gave some good advice here. Just to add to what he said, when you use your debugger, take note of where the exception is thrown. If it is on `con.Open();` then it is likely a problem with the connection string. If it is on one of the next 2 lines, it is probably a problem with your SQL (like, perhaps the table name is `Students` and not `Student` or the column name is not `StudId`). If using the debugger is not an option, then introduce a new string that says which line you visited last and print that, too. – David Apr 08 '14 at 04:03
  • i took debugger step by step...it skipped the steps after executenonquery()....but i crossed checked everythg many times...everythg is perfect...:( – user3447076 Apr 08 '14 at 04:14
  • OK, I was in the wrong spot. Which function were you in (btnInsert_Click, btnUpdate_Click, btnDelete_Click, etc.)? Also, what was your exception message (er.Message)? Was there an inner exception? – David Apr 08 '14 at 04:22
  • dude...i got it...the problem was i had set primary key identity as true...so the problem persisted of connection..thank you anyways ..(y) – user3447076 Apr 08 '14 at 09:14

0 Answers0