Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a usercontrol called AddRow and a form called TimeSheet ,Timesheet has a save_btn event, on click of save_btn the value enter in usercontrol should be written to database,but I am not able to access the value from usercontrol

below is the code

C#
public void save_btn_Click(object sender, EventArgs e)
    {

        SqlCommand cmdinsert = new SqlCommand("insert into TimeSheet (Project_Name ,Activity_Name,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday) values (@Project_Name,@Activity_Name,@Monday,@Tuesday,@Wednesday,@Thursday,@Friday,@Saturday,@Sunday) ", con);
        cmdinsert.Parameters.AddWithValue("@Project_Name",  add.project_cbox.SelectedIndex);
        cmdinsert.Parameters.AddWithValue("@Activity_Name", activity_cb.SelectedIndex);
        cmdinsert.Parameters.AddWithValue("@Monday", add.monday_txt_val);
        cmdinsert.Parameters.AddWithValue("@Tuesday", add.tuesday_txt_val);
        cmdinsert.Parameters.AddWithValue("@Wednesday", add.wednesday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Thursday", add.thursday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Friday", add.friday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Saturday", add.saturday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Sunday", add.sunday_txt.Text);
        con.Open();
        cmdinsert.ExecuteNonQuery();
        con.Close();  

    }


this event is in timesheet but the value is empty string...I just cant access the value,is there a solution? this is window application

C#
public string monday_txt_val
    {
        get
        {
            return monday_txt.Text;
        }

        set
        {
            monday_txt.Text = value;
        }
    }


monday_txt is a textbox that is created dynamically,after the value is entered ,but it is reading a empty string,despite me entering a value in textbox,when it is passed to form value of monday_txt is empty string,how to fix this?

only way I can think of accessing monday_txt is by creating a instance of AddRow in Timesheet..but When new instance is created I will not be able to get the value, kindly help
Posted

1 solution

You don't want to create an new instance - you need to get the actual instance of the UserControl that you created dynamically and presented to the user for him to fill in. There are two ways:
1) Keep a class level reference in your form class when you call new AddRow() to add the control,
2) Look through the Form.Controls collection to find the AddRow instance - remember that if you add it to a panel or other container, you will need to recurse through containers to find it.
 
Share this answer
 
Comments
arsr24 18-Jan-13 6:24am    
@OriginalGriff-hey!!,I know I shouldnt create a new instance,I am adding usercontrol dynamically,on a click_event the control is added,problem is if I dont create a instance of usercontrol error message is thrown,will you please explain it to me with code,it will help me great deal,also this is a windows app,I tried using findcontrol ,but I couldn`t find it..it would be easy for me to understand if you will explain with code..thank you
OriginalGriff 18-Jan-13 6:36am    
I can't because I don't know what you are doing when you add the control in the click event - but at that point you want to save the instance you create, because that is (probably, I can't see how your system works from here) the instance you need to query for the values - if you query any other instance, you will get blank values (or the wrong values if you have created more than one)
arsr24 18-Jan-13 6:41am    
@Griff,how do I do it Griff? please help me,if I dont create new instance of my usercontrol in my form how will I get to access the textboxes created in usercontrol?
OriginalGriff 18-Jan-13 6:51am    
You have to create an instance, but =when you create it you should keep a reference to it somewhere - or otherwise you don't know which one is which if you need a second at some point.
So in your click event handler you either need to save the new instance of a AddRow in a class (i.e. form) level variable as you create them, or perhaps in a class level List of AddRows if you can add more than one (and using a click event to add one does sound like you do - I'm guessing these are new rows of data for your user to enter and you need to save them all in a database when he says "ok")
arsr24 18-Jan-13 6:34am    
@OriginalGriff - Will you please elaborate,I keep a reference variable called count,but then I just dont know how to go about solving this issues,I am stuck here,any help would be much appreciated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900