none
C# and SQL RRS feed

  • Question

  • I type in 6 egg values in the textBox1(multiline). 
    I am having an issue of how do I get it to print out the egg values from the query to the serialnumberdatas 


     private void button1_Click(object sender, EventArgs e)
            {        
                egg = textBox1.Text; //egg will show "EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460"	
                int k = Convert.ToInt32(textBox2.Text);
    
                for (int i = 0; i < k; i++)
                {        
                serialnumberdata.DataSource = serialnumberdatas();               
                DataTable serialnumberdatas()
                {
                   DataTable dtSerial = new DataTable();
    
                    string connString = ConfigurationManager.ConnectionStrings["pie"].ConnectionString;
    
                    using (SqlConnection con = new SqlConnection(connString))
                    {
                       using (var cmd = new SqlCommand("select eggcounter From [Product].[Counter01unit]  where eggcounter = @egg", con))
                        {
                            cmd.Parameters.AddWithValue("@egg", egg);                       
                            con.Open();
                            SqlDataReader reader = cmd.ExecuteReader();
                            dtSerial.Load(reader);                
                        }
                            con.Close();
                        }                   
                        return dtSerial;                  
                    }
                }


    Wednesday, February 13, 2019 7:26 PM

All replies

  • Hi hardheadedvs,

    Thank you for posting here.

    According to your description, I think you want to search 6 values when eggcounter matches the value you input in the textBox1. k is the count of the values you input in textBox1.

    If yes, please try the code below. Due to I do not have your data, I test with my own database.

     private void button1_Click(object sender, EventArgs e)
            {
                string egg = textBox1.Text; //egg will show "EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460"	
                string[] s = egg.Split(',');
                int k = Convert.ToInt32(textBox2.Text);
    
                DataTable dtSerial = new DataTable();
                DataTable table = new DataTable();
    
                for (int i = 0; i < k; i++)
                {
                                       //ConfigurationManager.ConnectionStrings["pie"].ConnectionString;
                    string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database.mdf;Integrated Security=True";
    
                    using (SqlConnection con = new SqlConnection(connString))
                    {
                        using (var cmd = new SqlCommand("SELECT ItemCode,ItemName FROM Table1  where ItemCode = @egg", con))//select eggcounter From [Product].[Counter01unit]  where eggcounter = @egg
                        {
                            cmd.Parameters.AddWithValue("@egg", s[i]); //do not use egg, use s[i]
                            con.Open();
                            SqlDataReader reader = cmd.ExecuteReader();
                            dtSerial.Load(reader);
                            table = dtSerial.Clone();
                            foreach (DataRow dr in dtSerial.Rows)
                            {
    
                                table.Rows.Add(dr.ItemArray);
                            }
                        }
                        con.Close();
                    }
                }
    
    
                dataGridView1.DataSource = dtSerial;
            }

    When you input EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460 in TextBox1, you could use the code below to split.

    Change:
     string[] s = egg.Split(',');
                int k = Convert.ToInt32(textBox2.Text);
    To:
    string[] stringSeparators = new string[] { "\\r\\n" };//EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460
                string[] s = egg.Split(stringSeparators, StringSplitOptions.None);

    You will get a string array with 6 values. And then input 5 in textBox2 k value.

    Please change your code with comments in my code.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 14, 2019 3:42 AM
    Moderator