8 Replies - 2442 Views - Last Post: 26 April 2011 - 12:55 AM Rate Topic: -----

#1 Vishu Sukhdev   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Unable to getting value from textbox

Posted 25 April 2011 - 06:44 AM

Hi,
In my application i am assign value to textbox control at runtine but when i am trying to get value from textbox it shows null but when i assign value at design time it able to get value. i tried many option to solve this problem and cleaned solution and rebuild many times but still unable to get value. Can anybody tell me the reason.

(I have multiple controls on my form but excepting datetimepicker no other control getting value)

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to getting value from textbox

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,197
  • Joined: 12-June 08

Re: Unable to getting value from textbox

Posted 25 April 2011 - 07:03 AM

What property are you using to retrieve the data? What event are you setting the data? When are you retrieving it? Actually how about showing your code so we can determine what's up?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Unable to getting value from textbox

Posted 25 April 2011 - 07:35 AM

Not every control holds its contents in the .value property.
A textbox has its value in the .Text property.

string theContents = myTextBox.Text;


You can't just assume that because one control works one way they all work that way. I highly recommend you start with a book or on-line tutorial series and work from start to finish, rather than skip around trying to learn only what you need for this one project.

Standard resources, references and suggestions for new programmers.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

Free editions of Visual Studio 2010

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes

Working with environmental variables

Debugging tutorial
Debugging tips
Great debugging tips

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.


These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

Was This Post Helpful? 0
  • +
  • -

#4 romil797   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 24-April 11

Re: Unable to getting value from textbox

Posted 25 April 2011 - 05:35 PM

Yep, and ask the community if you need help. Also, experiment!
Was This Post Helpful? 0
  • +
  • -

#5 Vishu Sukhdev   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Unable to getting value from textbox

Posted 25 April 2011 - 11:06 PM

Hi,
This is my Code
 private void btnSave_Click(object sender, EventArgs e)
        {
            oEmployee = new cEmployee();
            
            string sName = textBox1.Text; //Took Textbox to assign its value to sName 

            if (oEmployee.IsEmptyString(txtName,
                "Employee Name Required...!")) return;
            if (oEmployee.IsEmptyString(cmbGender,
                "Select Gender ...!")) return;
            if (oEmployee.IsEmptyString(cmbBloodGroup,
                "Select Blood Group...!")) return;
            if (oEmployee.IsEmptyString(txtAddress,
                "Address Required...!")) return;
            if (oEmployee.IsEmptyString(txtPIN,
                   "Pincode Required...!")) return;

           
            if (txtGrossSal.Text == string.Empty) txtGrossSal.Text = "0";

            oEmployee.Name = txtName.Text.ToUpper().Trim();
            oEmployee.Gender = cmbGender.Text.Trim();
            oEmployee.DOB = dtpDOB.Value.Date;
            oEmployee.BloodGroup = cmbBloodGroup.Text.Trim();
            oEmployee.MaritualStatus = cmbMarital.Text.Trim();
            oEmployee.Address = txtAddress.Text.Trim();
            oEmployee.City = cmbCity.Text.Trim();
            oEmployee.Pincode = Convert.ToInt32(txtPIN.Text);
            oEmployee.State = cmbState.Text.Trim();
            oEmployee.Country = cmbCountry.Text.Trim();
            oEmployee.PhoneNo = txtPhone.Text.Trim();
            oEmployee.MobileNo = txtMOB.Text.Trim();
            oEmployee.EmailId = txtEmailID.Text.Trim();
            oEmployee.ImageName = "NOT SPECIFY";
            oEmployee.Status = 0;
            oEmployee.HigherEducation = cmbHigherEducation.Text.Trim();
            oEmployee.SkillSet = txtSkillSet.Text.Trim();
            oEmployee.Certification = txtCertification.Text.Trim();
            oEmployee.Others = txtOther.Text.Trim();
            oEmployee.PreviousCompany = txtExCompName.Text.Trim();
            oEmployee.Description = txtDescription.Text.Trim();
            oEmployee.FromDate = dtpFromDt.Value.Date;
            oEmployee.ToDate = dtpToDt.Value.Date;
            oEmployee.Designation = txtDesignation.Text.Trim();
            oEmployee.Responsibility = txtResponsibility.Text.Trim();
            oEmployee.CompanyName = cmbCompName.Text.Trim();
            oEmployee.DepartmentName = cmbDept.Text.Trim();
            oEmployee.DesignationName = cmbDesignation.Text.Trim();
            oEmployee.StartTime = Convert.ToDateTime(dtpCheckInTime.Value.ToShortTimeString());
            oEmployee.EndTime = Convert.ToDateTime(dtpCheckOutTime.Value.ToShortTimeString());
            oEmployee.OffDay = cmbOffDay.Text.Trim();
            oEmployee.Salary = Convert.ToDecimal(txtGrossSal.Text.Trim());
            oEmployee.ESICNo = txtESIC.Text.Trim();
            oEmployee.PANNo = txtPAN.Text.Trim();
            oEmployee.PFNo = txtPF.Text.Trim();


            oEmployee.DOLU = DateTime.Now;

            if (btnSave.Text == "&Save")
            {
                if (ExistingEmployeeName()) return;

                oEmployee.Insert_Update_Delete_Employee(1);
            }
            else if (btnSave.Text == "&Update")
            {
                oEmployee.ConfirmatinResult = oEmployee.MessageBoxConfirmation(
                    "Are You Sure You Want To Modify Employee Details ?");
                if (oEmployee.ConfirmatinResult == DialogResult.Yes)
                {
                    oEmployee.Insert_Update_Delete_Employee(2);
                }
            }

            btnClear_Click(sender, e);
        }


when i failed every way i took one textbox1 and past it to form i tried to get value but still got null in string sName after click btnSave (i debug all code )
Was This Post Helpful? 0
  • +
  • -

#6 Zdrenka   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 116
  • Joined: 24-November 09

Re: Unable to getting value from textbox

Posted 25 April 2011 - 11:53 PM

where are you using sName? looks like you have set it to the value of the textbox but then you havnt used it anywhere.. maybe im missing somthing though.
Was This Post Helpful? 0
  • +
  • -

#7 Vishu Sukhdev   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Unable to getting value from textbox

Posted 26 April 2011 - 12:09 AM

View PostZdrenka, on 26 April 2011 - 06:53 AM, said:

where are you using sName? looks like you have set it to the value of the textbox but then you havnt used it anywhere.. maybe im missing somthing though.

i just declare sName at 3 line of my code for only checking . i unable to getting value of my controls to their associate properties that's why i took extra Textbox(textbox1) and trying to assign value to sName . when i am debugging my code using f10 and check value of sName it shows null.
Was This Post Helpful? 0
  • +
  • -

#8 Rico Diesel   User is offline

  • D.I.C Head

Reputation: 63
  • View blog
  • Posts: 122
  • Joined: 06-May 10

Re: Unable to getting value from textbox

Posted 26 April 2011 - 12:22 AM

if sName is only assigned, but never used in your code it will be ignored by the compiler (optimization).

Or start using sName somewhere in your code, or simply write an if loop after the assignment which involves sName to check it, but IMHO it would be better to check Textbox1.Text directly when you need to assign it to something in your Employee class and simply remove the extra variable.

string sName = Textbox1.Text;
if (sName == string.Empty)
{
  MessageBox.Show(sName);
}
else
{
  MessageBox.Show('Textbox1 is empty!');
}


This post has been edited by eclipsed4utoo: 26 April 2011 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#9 Vishu Sukhdev   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Unable to getting value from textbox

Posted 26 April 2011 - 12:55 AM

Thank you For your suggestion Friends
i never got any solution of my problem so Finally i replaced my all design with my existing form and now it work fine
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1