Populate text box from previous form to new form

Ok, I am working on 2 forms the first I want to have a single text box with a control that says "look up" The user will enter a number in my case an isbn number. I want a second form to open up populating isbn number again from the previous form. The goal after is to use a select statement to return the column associated with that isbn number ie (the title, author, and description of the book ) I think I can figure that out since I just need the isbn value for my select statement. ( but if you want to help me there that would be sweet the second form would have a Title, Author, ISBN, and Description) I have looked around but cant seem to find an example related to my situation.  Thanks guys!
linkcube1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systanCommented:
just_this_way;_if_your_on_other_form?,
Form2 f2;
f2.textbox1.text="value";
0
jdavistxCommented:
This is a pretty quick and simple example, but maybe it'll get you started.

//Some class for our Book info
public class Book
{
	string ISBN {get; set;}
	string Title {get; set;}
	string Author {get; set;}
	string Description {get; set;}
	
	public Book()
	{
		ISBN = "Missing";
		Title = "Missing";
		Author = "Missing";
		Description = "Missing";
	}
}

//On the form with the "Look Up" option
//Assuming some _Click event from a button the user clicks after enter an ISBN number
LookupButton_Click(object sender, EventArgs e)
{
	using(var frm = new BookInfoForm(LookupBook(ISBNTextBox.Text));
		frm.ShowDialog();
}

private Book LookupBook(string isbn)
{
	Book b = new Book();
	b.ISBN = isbn;
	//Query for your book info
	try
	{
		using(var con = new SqlConnection("your connection string"))
		{
			con.Open();
			DataTable book;
			var adapter = new SqlDataAdapter(String.Format("SELECT Title, Author, Description FROM MyBookTable WHERE Isbn = '{0}'", b.ISBN), con);
			adapter.Fill(book);
			
			if(result.Rows > 0)
			{
				b.Title = book["Title"].ToString();
				b.Author = book["Author"].ToString();
				b.Description = book["Description"].ToString();
			}
		}
	}
	catch(Exception e)
	{
		//Exception handling
	}
	return b;
}

//On the BookInfoForm
//Make a constructor that accepts a book
public BookInfoForm(Book b)
{
	InitializeComponent();
	this.Text = String.Format("Book Info for ISBN {0}", b.ISBN);
	//Populate your fields with the book object
	AuthorTextBox.Text = b.Author;
	//etc.
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdavistxCommented:
Err
if(result.Rows > 0)
{
	b.Title = book["Title"].ToString();
	b.Author = book["Author"].ToString();
	b.Description = book["Description"].ToString();
}

Open in new window


should be

if(book.Rows > 0)
{
	b.Title = book.Rows[0]["Title"].ToString();
	b.Author = book.Rows[0]["Author"].ToString();
	b.Description = book.Rows[0]["Description"].ToString();
}

Open in new window


Sorry
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

linkcube1Author Commented:
ok Thanks for the quick answer jdavistx I tried you code and posted What I have.

MY SCAN BOOK FORM

Open in new window

public partial class FrmScanBook : Form
    {
        Form formParent = null;
       
        public FrmScanBook(Form par)
        {  
            formParent = par;
            InitializeComponent();
        }
                 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            formParent.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmNewItem frm = new frmNewItem(LookupBook(txtEnterISBN.Text));
                frm.ShowDialog();
           
        }
        private Book LookupBook(string ISBNNumber)
        {
            Book b = new Book();
            b.ISBNNumber = ISBNNumber;
            //Query for your book info
            try
            {              
                SqlConnection con = new SqlConnection("Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;" +
                   "Integrated Security=True");
                {
                    con.Open();
                    DataTable book;
                    var adapter = new SqlDataAdapter(String.Format("SELECT Title, Author, Description FROM Books WHERE ISBNNumber = '{0}'", b.ISBNNumber), con);
                    adapter.Fill(book);

                    if (book.Rows > 0)
                    {
                        b.Title = book.Rows[0]["Title"].ToString();
                        b.Author = book.Rows[0]["Author"].ToString();
                        b.Description = book.Rows[0]["Description"].ToString();
                       
                      //  b.Description = book["Description"].ToString();
                    }
                }
            }
            catch (Exception e)
            {
                //Exception handling
            }
            return b;
        }
    }
}

Open in new window

I'm getting one error "Error      1Operator '>' cannot be applied to operands of type 'System.Data.DataRowCollection' and 'int'      "
from this line if (book.Rows > 0)

0
systanCommented:
con.Open();
                    DataTable book=new DataTable();
...
...
adapter.Fill(book);
                    if (book.Rows > 0)
...
...
0
linkcube1Author Commented:
Still same error
0
systanCommented:
not_sure_of_this;__try_to_rename_book__to__mybook,++incase_for_bug?__no_harm_in_trying
DataTable mybook=new DataTable();

because_you_have_the_same_name_with;
Book b = new Book();

OR
just_change_with_this;
...
DataTable book=new DataTable();
...
if (book.Rows >= 0)
...
///because_rows_starts_at_0__not_1
0
jdavistxCommented:
Sorry, it should be book.Rows.Count > 0

Give that a shot.
0
linkcube1Author Commented:
Ok I enter an ISBN number into the first form and the following line txtISBNNumber.Text = b.ISBNNumber;
 It gives me a nullrefrence exception which is shown in the picture.

Open in new window

 public frmNewItem(Book b)
        {
            this.Text = String.Format("Book Info for ISBN {0}", b.ISBNNumber);
            //Populate your fields with the book object      
            txtISBNNumber.Text = b.ISBNNumber;
        }

Open in new window



Screen-shot-2011-03-30-at-3.35.0.png
0
jdavistxCommented:
A couple things...

First, what are you using those two private Books that have scope throughout frmNewItem? If you want frmNewItem to have a reference to your Book, then do this:

public partial class frmNewItem : Form
{
	private Book book;
	
	public frmNewItem(Book b)
	{
		this.book = b;
		//do stuff with book
	}
	
	//...
}

Open in new window


Do the above, and also make sure that the code that instantiates frmNewItem is passing a valid book object.  So, wherever you're doing the part with...
frmNewItem frm = new FrmNewItem(myBook)

Open in new window


...make sure myBook actually exists
0
linkcube1Author Commented:
Ok I changed the code to the following

Open in new window


        private Book book;

        public frmNewItem(Book b)
        {
            this.book = b;
            this.Text = String.Format("Book Info for ISBN {0}", b.ISBNNumber);
            b.ISBNNumber = txtISBNNumber.Text;
        }      

Open in new window


Still null...


           
0
linkcube1Author Commented:
I dont know but maybe this screenshot will be of use
Screen-shot-2011-03-30-at-6.10.0.png
0
linkcube1Author Commented:
Nvm fixed it....

It was opening a blank from not the form I made
added "InitializeComponent();" must have deleted it and it populates the ISBN and the other items from the SQL query, thanks jdavistx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.