-2

i have tried to insert the data by login to the system. my query doesn't have any error, but the exception has thrown by the run time as "Object reference not set to an instance of an object ". check my code and please correct me.

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (TextBox6.Text == " ")
            {
                string alertmessage = "";
                alertmessage = "Username should not be blank";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox6.Focus();
            }
            else if (TextBox7.Text == " ")
            {
                string alertmessage = "";
                alertmessage = "Username should not be blank";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox7.Focus();
            }
            else
            {
                string sq = "SELECT COUNT(*) FROM tbl_KKSUser WHERE Uname=@un and Password=@pas";
                SqlCommand sd = new SqlCommand(sq, con);
                SqlParameter unameparam;
                unameparam = new SqlParameter("@un", SqlDbType.VarChar, 25);
                unameparam.Value = TextBox6.Text;
                sd.Parameters.Add(unameparam);

                string original = TextBox7.Text.Trim();
                string withhash = original;
                b1 = Encoding.BigEndianUnicode.GetBytes(withhash);
                encrypted = Convert.ToBase64String(b1);
                SqlParameter passparam;
                passparam = new SqlParameter("@pas", SqlDbType.VarChar, 8000);
                passparam.Value = Convert.ToString(encrypted);
                sd.Parameters.Add(passparam);

                con.Open();
                {
                    int iresults;
                    iresults = Convert.ToInt32(sd.ExecuteScalar().ToString());
                    if (iresults > 0)
                    {
                        string q = "insert into tbl_KKSMaterialRaise(MaterialCode,Source,Category,Population,StockInStores,Specification,PrearedBy,CheckedBy,ApprovedBy,CreatedDate) values(@mc,@sc,@cat,@pop,@sis,@spec,@pb,@cb,@ab,@cd)";
                        SqlCommand dm = new SqlCommand(q, con);
                        dm.Parameters.AddWithValue("@mc", Mcodeddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@sc", TextBox1.Text.Trim());
                        dm.Parameters.AddWithValue("@cat", TextBox2.Text.Trim());
                        dm.Parameters.AddWithValue("@pop", TextBox3.Text.Trim());
                        dm.Parameters.AddWithValue("@sis", TextBox4.Text.Trim());
                        dm.Parameters.AddWithValue("@spec", TextBox5.Text.Trim());
                        dm.Parameters.AddWithValue("@pb", PBddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@cb", CBddl.SelectedItem.Text);//In this line i have got error
                        dm.Parameters.AddWithValue("@ab", ABddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@cd", DateTime.Today);
                        dm.ExecuteNonQuery();
                        string alertmessage = "";
                        alertmessage = "Component Details Saved";
                        this.CreateMessageAlert(this, alertmessage, "alertKey");
                    }
                    else
                    {
                        Response.Write("<script>alert('Invalid Username/Password')</script>");
                    }
                }
                con.Close();

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
user1455232
  • 43
  • 2
  • 10

2 Answers2

2

It looks most likely that one of your dropdownlists has no option selected, i.e. the null reference is coming from one of the lines like:

dm.Parameters.AddWithValue("@mc", Mcodeddl.SelectedItem.Text);

Try checking that all of those have items selected before retrieving the .Text property.

If it isn't that, it would be useful to know which line is causing the exception - you can usually get that from the exception stack trace.

Rophuine
  • 724
  • 3
  • 8
0

It means you have not initialized or assigned a variable. The debugger should tell you which variable specifically. Take a closer look at it. Then you only have to check that it is already initialized (= new Class()) or assigned (= instance).

You may want to take a look at this question.

Community
  • 1
  • 1
Dil
  • 632
  • 7
  • 13
  • This is best fit as a comment adds no value as an answer – V4Vendetta Jun 26 '12 at 06:48
  • @V4Vendetta, I disagree, I am explaining what kind of error it is, so he can find it himself. It is much easier for him to check what the debugger says than for us. And thus he will know better next time. – Dil Jun 26 '12 at 06:50
  • @user1455232 I am not sure, maybe this is a special case, but I think the debugger should tell you exactly which reference "is not set to an instance of an object", at least if you are using Visual Studio. See which one it is and follow it back to its origin to see if it is assigned any value. – Dil Jun 26 '12 at 07:41
  • It could as well be referring to the SelectedItem, as Rophuine said. Check if all relevant lists have a selected item. – Dil Jun 26 '12 at 07:44
  • have got an error in this line " dm.Parameters.AddWithValue("@cb", CBddl.SelectedItem.Text);" – user1455232 Jun 26 '12 at 08:20
  • @user1455232 Then Rophuine was right. Check that CBddl has a SelectedItem before running the query. If CBddl.SelectedIndex == -1 then there is no SelectedItem, and that is why you have an error. You can accept his answer. – Dil Jun 26 '12 at 10:00