Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Forms
hello
 

hope you all are doing well !
 

i have two tables
- book
contain book id
and
- ss
contain book id and Snumber
 
I want the user to get the form from the book id when he insert the Snumber in textBox1
 
this is the code
 
private void button1_Click(object sender, EventArgs e)
        {
            
 

            String constring = "Data Source=V-PC;Initial Catalog=SMA;Integrated Security=True";
            SqlConnection con1 = new SqlConnection(constring);
           
 
            String Search = "Select bookID from ss where Snumber='" + this.textBox1.Text + "'";
           
            SqlCommand SearchS = new SqlCommand(Search, con1);
            SqlDataReader myreader;
            
 
            try
            {
                con1.Open();
                myreader = (SearchS.ExecuteReader());
                
 

                if (Search =="1")
                {
                    this.Hide();
                    S1 d1 = new S1();
                    d1.ShowDialog();
                }
                else if (Search == "2")
                {
                       this.Hide();
                   S2 e3 = new S2();
                    e3.ShowDialog();
                }
                /*else if   ( Search == "3")
                {
                       this.Hide();
                   S3 e3 = new S3();
                    e3.ShowDialog();
                }*/
 
                
 
                while (myreader.Read())
                {
                }
 

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
            con1.Close();
        }
        }
 

there is no error
but in debugging when I press the button that contain this code
 
the button do nothing !!
 

 
Did I do something wrong or forget something ?
Posted 28-Jun-13 6:28am
09lolo160
Comments
ThePhantomUpvoter at 28-Jun-13 11:35am
   
What does "the button do nothing" mean? The click event does not run? It runs but does not do what you expect? What is it that you expect? What does it actually do?
09lolo at 28-Jun-13 11:50am
   
the button should take me to another form when he insert his Snumber.
ThePhantomUpvoter at 28-Jun-13 11:48am
   
Your If statements are completely wrong. Search is never going to be equal to "1", "2" or "3" because you have set Search to be your SELECT statement and there is nothing in your code that changes that.
09lolo at 28-Jun-13 11:52am
   
I know I did something wrong that's why I came here : )
I am just a beginner.
 
Thank you for your time.
ThePhantomUpvoter at 28-Jun-13 11:55am
   
And I am just telling you what you did so you can fix it and learn from your experience.
09lolo at 28-Jun-13 11:58am
   
Thank you I really appreciate your help , I will fix my code now . ^_^
ThePhantomUpvoter at 28-Jun-13 11:58am
   
You're welcome
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Well, I'm not really surprised! Lets just take "irrelevant" code away:
private void button1_Click(object sender, EventArgs e)
        {
...
            String Search = "Select bookID from ss where Snumber='" + this.textBox1.Text + "'";
...           
                if (Search =="1")
                {
...
                }
                else if (Search == "2")
                {
...
                }
...
        }
Since Search will never equal either value it does nothing with it.
 
Probably what you want to do is to use the values in the myreader returned data to make those decisions...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

int x /*define it global*/
while (myreader.read())
{
    x = int.parse(myreader[0].ToString());
}
if (x =="1")
                {
                    this.Hide();
                    S1 d1 = new S1();
                    d1.ShowDialog();
                }
I have given you a solution as much I have understood your question
hope this will help you
 

[edit]Code block fixed, spelling[/edit]
  Permalink  
v2

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 28 Jun 2013
Copyright © CodeProject, 1999-2014
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