0

Can anyone help me with this issue. i am trying to insert data in a local DB from my .NET app but it throws me Object Reference Exception. You can se my code below:

protected void Save_Click(object sender, EventArgs e)
    {

        try
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Rights"].ConnectionString);
            con.Open();
            string insert = "insert into userinfo(Name,Mbiemri) values (@Name,@Mbiemri)";
            SqlCommand cnd = new SqlCommand(insert, con);
            cnd.Parameters.AddWithValue("@Name", TextBox1.Text);

            cnd.Parameters.AddWithValue("@Mbiemri", TextBox2.Text);
            cnd.ExecuteNonQuery();
            Response.Redirect("Home.aspx");
            con.Close();

        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }

and i think that this is the line that throws the exception SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Rights"].ConnectionString);

please give me some solution cuz there are days i am working with this.

Lara
  • 179
  • 2
  • 11
  • Hint - what is `ConfigurationManager.ConnectionStrings["Rights"]` is null? – Gilad Green Oct 16 '17 at 07:13
  • Then the likely problem is the connectionstrings portion... – BugFinder Oct 16 '17 at 07:14
  • @GiladGreen I have no idea why that row is null. i have used the same soloution in different programs and worked just fine – Lara Oct 16 '17 at 07:16
  • @Lara - that is why you need to debug and find the case when it fails and why :) the dup question explains it all – Gilad Green Oct 16 '17 at 07:17
  • 1
    Are you have connection string name "Rights" using `` in app/web.config? – Tetsuya Yamamoto Oct 16 '17 at 07:17
  • @Lara Can you post the web.config part here. – Sankar Oct 16 '17 at 07:18
  • @Sanrar Raj – Lara Oct 16 '17 at 07:24
  • @Lara As you said you are trying to connect local DB. Then your LocalDB ConStr should be `` Problem is you have no ConStr with name of `Rights`. This the cause of `Object Reference Exception`. – Sankar Oct 16 '17 at 07:33
  • @SankarRaj i alredy tried this and leads me to this error HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory. – Lara Oct 16 '17 at 07:36
  • @Lara This is your next problem. You have not configured `Default Document` in IIS. You should fix the ConStr error first, Then look into this. – Sankar Oct 16 '17 at 07:37

0 Answers0