10 Replies - 841 Views - Last Post: 12 July 2011 - 10:08 AM Rate Topic: -----

#1 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 07:34 AM

i try something like this but it not work out as i want can help me see where to change

i have this datagridview with data contain in it(which i bind it from database)
and a combo box which contain a list of class, like example 6A, 6b....and so on. Then,when user select a class and click on a button submit it should help me **tag the student to a index number in sequence and insert into database**


this is the data grid view

Class| StudentDetail

6A| Jane,14 May 1980;Mary,4 June 1980; Sam, 15 Dec 1980;
6B| leen, 31 May 1980; Peter 6 Jan 1980;
6C| Eillen, 19 Aug 1980; Yvwon, 28 Mar 1980;
6D| Evan, 24 Dec 1980; Ivan, 20 Nov 1980;

so the database value should look like this

PrimaryKey| Detail(Name,IndexNo;)
1 | Jane,1; Mary,2; Sam, 3;
2 | Leen,4; Peter, 5;
3 | Eillen, 6; Yvwon, 7;
....

but i not able to get this
what i get is only this

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,1;Sam


this is not what i want, so how to change to what i want?
how to change the code?

this is the code i use for form


 private void btnInsert_Click(object sender, EventArgs e)
   {
 if (combobox1.Text == "6A")
     {
      var testresult = Convert.ToString(datagridview1.CurrentRow.Cells[0].Value);
      
      var result = Regex.Matches(testresult, @".*?,(.*?),.*?;").Cast<Match>().Select(arg => arg.Groups[1].Value).ToList();
      var resultString = string.Join(","+Student.GetIndexNo()+";", result );
     
      SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True");
      conn.Open();
      String str = "INSERT INTO students( Name )VALUES ('" + resultString + "') ";
      SqlCommand cmd = new SqlCommand(str, conn);
      cmd.ExecuteNonQuery();
 
      conn.Close();
      
      MessageBox.Show("inserted");
     }
     else
      return;
 
 }



this is the class(.cs)

public static class Student
  {
   static int indexNum = 0;
   static Student
   {
    using (SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True"))
    {
     string sql = "SELECT MAX(StudentID) FROM students"; //studentid is just the primary key
 
     SqlCommand cmd = new SqlCommand(sql, conn);
 
     conn.Open();
 
     indexNum = Convert.ToInt32(cmd.ExecuteScalar());
     conn.Close();
 
    }
   }
    public static string GetIndexNo()
   {
    ++IndexNum;
    return IndexNum.ToString();
   }
  }



thanks in advance and hope someone can help me with this

This post has been edited by cutexxbaby: 12 July 2011 - 07:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how to display the data regarding when user select any value in combo?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 07:52 AM

A lot of talk, with very little explanation of the actual problem details.

Quote

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,2; Sam, 3;
2 Leen,4; Peter, 5;
3 Eillen, 6; Yvwon, 7;
....

but i not able to get this
what i get is only this

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,1;Sam


Are you saying that all the entries come back with '1' as the index number instead of 1, 2, 3, ... ?
Because that is the difference I am seeing between the two data samples.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 07:58 AM

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. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
  • Learn how to search the 'net for examples. How to do a good search that will get you targeted answers.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    Form1_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 08:07 AM

Here's where you make the string you display.

var resultString = string.Join(","+Student.GetIndexNo()+";", result );


So you are getting Student.GetIndexNo().

Let's look at where that comes from.
public static string GetIndexNo()
   {
    ++IndexNum;



Where is IndexNum coming from? We don't see that variable anywhere in the code you provided.

I'm going to guess that it is really indexNum or a property drawn from indexNum. But it does show us that you aren't giving us the real code you're using - this can't work when IndexNum is not defined.

Since indexNum is set to 0 when you make a new Student and you increment it one time - that means every student always has an IndexNum of 1

public static class Student
  {
   static int indexNum = 0;


This post has been edited by tlhIn`toq: 12 July 2011 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 08:20 AM

View PosttlhIn`toq, on 12 July 2011 - 07:52 AM, said:

A lot of talk, with very little explanation of the actual problem details.

Quote

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,2; Sam, 3;
2 Leen,4; Peter, 5;
3 Eillen, 6; Yvwon, 7;
....

but i not able to get this
what i get is only this

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,1;Sam


Are you saying that all the entries come back with '1' as the index number instead of 1, 2, 3, ... ?
Because that is the difference I am seeing between the two data samples.


yes you are right
the first one is what i want
the second is what i get from the code, i cant make it to 1,2,3 and you see after sam it miss out 3 and semi colon
Was This Post Helpful? 0
  • +
  • -

#6 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 08:26 AM

View PosttlhIn`toq, on 12 July 2011 - 08:07 AM, said:

Here's where you make the string you display.

var resultString = string.Join(","+Student.GetIndexNo()+";", result );


So you are getting Student.GetIndexNo().

Let's look at where that comes from.
public static string GetIndexNo()
   {
    ++IndexNum;



Where is IndexNum coming from? We don't see that variable anywhere in the code you provided.

I'm going to guess that it is really indexNum or a property drawn from indexNum. But it does show us that you aren't giving us the real code you're using - this can't work when IndexNum is not defined.

Since indexNum is set to 0 when you make a new Student and you increment it one time - that means every student always has an IndexNum of 1

public static class Student
  {
   static int indexNum = 0;


from what i do , i thought that the IndexNum come from static int indexNum = 0; plus i sorry i type to fast the IndexNum is = indexNum
i am sorry because i a newbiest, this is really my code is just that some of the variable name i change, i not very good in coding, that why i try out but cant get the thing i want, so come to ask for help to see what wrong with the code
hope you can teach me and show me how to change it...really appreciate it

This post has been edited by cutexxbaby: 12 July 2011 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 08:46 AM

View PosttlhIn`toq, on 12 July 2011 - 07:52 AM, said:

A lot of talk, with very little explanation of the actual problem details.

Quote

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,2; Sam, 3;
2 Leen,4; Peter, 5;
3 Eillen, 6; Yvwon, 7;
....

but i not able to get this
what i get is only this

PrimaryKey Detail(Name,IndexNo;)
1 Jane,1; Mary,1;Sam


Are you saying that all the entries come back with '1' as the index number instead of 1, 2, 3, ... ?
Because that is the difference I am seeing between the two data samples.

like what i say there is one data that i want , the other one is what i get from the code i done, so basically how should i change in order to get what i want
hope you can help me as soon as possible
thanks
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 09:18 AM

The reason you keep getting '1' is because each instance of the Student class makes its own IndexNum starting at 0.

Therefore, you need to NOT have each Student make its own IndexNum - or to share a common IndexNum.

You need to stop initializing IndexNum within the Student class, and stop incrementing in the Student class.

You need to make an index number inside your main application, and increment it only from the main application, then send that number to your Student instances.

I don't see where you are making new Students. But it should look something like this...

Student newStudent = new Student()
                  {
                      newStudent.IndexNum = ++StudentCount
                  };


Do you see how your main application assigns the Student a unique IndexNumber?
Was This Post Helpful? 0
  • +
  • -

#9 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 09:31 AM

View PosttlhIn`toq, on 12 July 2011 - 09:18 AM, said:

The reason you keep getting '1' is because each instance of the Student class makes its own IndexNum starting at 0.

Therefore, you need to NOT have each Student make its own IndexNum - or to share a common IndexNum.

You need to stop initializing IndexNum within the Student class, and stop incrementing in the Student class.

You need to make an index number inside your main application, and increment it only from the main application, then send that number to your Student instances.

I don't see where you are making new Students. But it should look something like this...

Student newStudent = new Student()
                  {
                      newStudent.IndexNum = ++StudentCount
                  };


Do you see how your main application assigns the Student a unique IndexNumber?

anyway just to tell you the true is that i only have a button with a datagridview(which all the insert value will be show) so basically this what i have
public form1()
{
InitializeComponent();
}
private void Form_Load(object sender, EventArgs e)
{
bindcombobox();
}
protect void bindcombobox()
{
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=studentList;Integrated Security=True");

 SqlDataAdapter adapter = new SqlDataAdapter("SELECT StudentName, Class FROM StudentAtt", conn);

 DataSet ds = new DataSet();

 adapter.Fill(ds);

 combobox1.DataSource = ds.Tables[0];
 combobox1.DisplayMember = "Class";
 combobox1.ValueMember = combobox1.DisplayMember;

}


can i know where that Student come from where you declare Student newStudent = new Student()
and where did you get a student count++
i kind of dont get what the code, can show a bit detail
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 09:55 AM

In your code YOU must be making instances of Student someplace. And you must be tracking how many you have made, right? Either by adding them to a List<Student> or some other collection. Right? But you did not show us that code.

The code I showed you in an EXAMPLE of how you would make new Students and assign a unique index number to them from the main program at the time you make the Student instance. StudentCount is a an EXAMPLE variable indicating that you need to keep tract of the count of Students.

You can't expect me to make your finished code for you with all the variable names exactly matching your code - especially when you haven't shown where you make your Student instances.

Let me ask:
  • Did you write this program you are showing us? Or is it a collection of code you copy/pasted from other sources and now you're trying to make it all work together?
  • Is this a homework assignment? Is it a group project where some of the code came from other students so you don't understand what they wrote?

Was This Post Helpful? 0
  • +
  • -

#11 cutexxbaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-June 11

Re: how to display the data regarding when user select any value in combo?

Posted 12 July 2011 - 10:08 AM

View PosttlhIn`toq, on 12 July 2011 - 09:55 AM, said:

In your code YOU must be making instances of Student someplace. And you must be tracking how many you have made, right? Either by adding them to a List<Student> or some other collection. Right? But you did not show us that code.

The code I showed you in an EXAMPLE of how you would make new Students and assign a unique index number to them from the main program at the time you make the Student instance. StudentCount is a an EXAMPLE variable indicating that you need to keep tract of the count of Students.

You can't expect me to make your finished code for you with all the variable names exactly matching your code - especially when you haven't shown where you make your Student instances.

Let me ask:
  • Did you write this program you are showing us? Or is it a collection of code you copy/pasted from other sources and now you're trying to make it all work together?
  • Is this a homework assignment? Is it a group project where some of the code came from other students so you don't understand what they wrote?

basically you know why i ask you why you create Student because i bind the combo box with the database that why i don't think i need to instance cause if i use instance , basically i doing hard coded
next this is my assignment, i did this code and post this code on other forum but seem like no body get what i mean so i post here thought someone can help,
i dont intend you to finish my work , i just need your help to tell me where should i change and change to what, cause as a newbiest i not very sure with my code
for the student count, must i create a var to store it first something like static int studentNum =o;
next is where the data that i get behind "Sam" dont have the indexNum and the semi colon?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1