Click here to Skip to main content
11,931,104 members (58,426 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# User-controls
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

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);

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

public string monday_txt_val
            return monday_txt.Text;
            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 18-Jan-13 0:50am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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 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
arsr24 18-Jan-13 7:04am
@Griff,That is correct Griff,but when a row is created (which are textboxes) user enters the value ,but when I trigger the click_event in the form ,all I get in "monday_txt.Text" is a empty string..but if I create a click event in the usercontrol it works fine,I am stuck with this part from four days,how do I use the reference variable value? and how do I write it to database?
OriginalGriff 18-Jan-13 7:08am
Post your click event handler and I'll have a quick look.
arsr24 18-Jan-13 7:11am
private void Edit_btn_Click(object sender, EventArgs e)

AddRow add = new AddRow();
save_pn.Location = new Point(294, 523);


private void Add_Row_Click(object sender, EventArgs e)
var add = new AddRow();
add.Top = count * (add.Height-50) ;
add.AutoScroll = true;
remove_btn.Enabled = true;
also the Save_btn posted in my code above is the event using which I have to write into database,the code with the queries
arsr24 18-Jan-13 7:23am
@griff,sir,please help me fix this issue
OriginalGriff 18-Jan-13 7:50am
Ok - you are adding the new control instances into a panel - the show_pnl.
So, you could run through them:
foreach (Control cin show_pan.Controls)
AddRow ar = c as AddRow;
if (ar != null)
// ar is an instance of the AddRow controls you added - look at the monday_txt_val property of that.

How would you normally save into a database? And which database system?
arsr24 19-Jan-13 3:06am
@griff,mysql 2008,on save_btn click it should be saved into database,that is how I try adding to database
OriginalGriff 19-Jan-13 3:09am
Yes, but what code would you normally use? I don't want to suggest a solution involving SqlCommand objects if you normally use an SqlDataAdapter for example.
arsr24 19-Jan-13 3:55am
what code I use, as in? I didnt understand what you meant Griff?
arsr24 19-Jan-13 4:01am
@Griff,If I create a local click_event the values gets saved to database,say ,I create a save_btn in the user control,but when I create a save event in the form the value of the textbox is empty,I debugged ,the value is passed to the form when I trigger the save_event in the usercontrol but otherwise it a empty string
OriginalGriff 19-Jan-13 4:12am
I mean, show the code you use to save the data to teh database - probably the "local click event" is that is what is working.
arsr24 19-Jan-13 6:39am
@griff ,the above code posted in the question,when I paste the the same code in usercontrol, it adds to the database
OriginalGriff 19-Jan-13 6:52am
The chances are that when you use the code in the UserControl "add" is defined and refers to the active instance in some way.
When you execute it outside the Control, you have to refer to the control instance in order to get the textbox with info in it. If you create a new one, or look at a different instance it will likely be blank (or wrong)
arsr24 19-Jan-13 12:54pm
@griff,how do I do that? I really dont know how I do it,I know it`s because I am creating a new instance,I know why the problem is ,I just dont know how to work around it...can you please help?
OriginalGriff 20-Jan-13 5:10am
Not really - I can't see your code, or your screen, so I have no idea what you are doing with your controls.
It may be that you need to look through the panels Controls list as I suggested yesterday to find the instance.
arsr24 19-Jan-13 3:07am
@Griff,But save_btn is in the form,AddRow is my usercontrol

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100