0

I dynamically create controls which I need
Here is the code 

public void cmdButton1_OnClick(object sender, EventArgs e)
{

        Label myLabel = new Label();
        myLabel.ID = "lblNameL" + i.ToString();
        myLabel.Text = "Трите имена на латиница ";
        TextBox myTextBox1 = new TextBox();
        myTextBox1.ID = "txtNameL" + i.ToString();
        Page.FindControl("form1").Controls.Add(myLabel);
        Page.FindControl("form1").Controls.Add(myTextBox1);

        Label mylabel2 = new Label();
        mylabel2.ID = "lblNameK" + i.ToString();
        mylabel2.Text = "Трите имена на кирилица";
        TextBox myTextBox2 = new TextBox();
        myTextBox2.ID = "txtNameK" + i.ToString();
        Page.FindControl("form1").Controls.Add(mylabel2);
        Page.FindControl("form1").Controls.Add(myTextBox2);
}

And here i try to execute sql query so that I can insert what's written the textbox txbNameK into table Tourist the exception is in the row cmd.Parameters.add

public void cmdInsert_OnClick(object sender, EventArgs e)
{

        TextBox tx888 = (TextBox)FindControl("txtNameK" + i.ToString());
        TextBox tx99 = (TextBox)FindControl("txtNameL" + i.ToString());

        string insertSQL = "INSERT INTO Tourist ( Name_kir, Name_lat) VALUES (@Name_kir, @Name_lat, )";

        string connectionString = "Data Source = localhost\\SQLExpress;Initial Catalog=Pubs;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(insertSQL, con);
        cmd.Parameters.AddWithValue("@Name_kir",tx888);
        cmd.Parameters.AddWithValue("@Name_lat", tx99);

        int added = 0;
        try
        {
            con.Open();
            added = cmd.ExecuteNonQuery();
            lblResult.Text = added.ToString() + "records added";

        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message;
        }
        finally
        {
            con.Close();

        }
    }
}
1
  • You've never checked whether you actually found the textbox, my money's on that being null. Further, I assume you actually mean to pass the "Text" property of the textbox, not the whole thing? Commented Nov 21, 2012 at 13:05

2 Answers 2

1

Two things:

TextBox tx888 = (TextBox)FindControl("txtNameK" + i.ToString());
TextBox tx99 = (TextBox)FindControl("txtNameL" + i.ToString());
if(tx888 == null)
    return;
if(tx99 == null)
    return;

Above will check if your texbox is null or not And another thing is:

cmd.Parameters.AddWithValue("@Name_kir",tx888.Text);
cmd.Parameters.AddWithValue("@Name_lat", tx99.Text);
Sign up to request clarification or add additional context in comments.

2 Comments

HI! Thank you for your answer! I've tried this before - but still the exception stays!
now everything is right! I just had some problems with the query!
0

Object reference exception always occurs whenever null object is trying to access any value. eg..

Code below will work fine.

Exception ex = new Exception()
Textbox1.Text = ex.Message;

Code below will throw an exception

Exception ex= null;    //one and the same thing

Textbox1.Text = ex.Message;     //Object reference not set....exception will be raised since ex is null

This is what is happening in your code. Try to place a breakpoint and debug it. For best practice always check for an object for null as suggested by Prashant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.