Split string and bind it to GridView in ASP.Net

Last Reply on Jan 04, 2014 05:31 AM By Pradip

Posted on Jan 04, 2014 02:59 AM

I have a string of CustomerId and Cusotmername like..

"1233Adsf 48872Hjyu 1289Rtop

Now how  to show this record in Data Gridview??

Help me..plzzzz??

Thanks!!

You are viewing reply posted by: Pradip on Jan 04, 2014 05:31 AM.
Posted on Jan 04, 2014 05:31 AM

Try This

 

        private void button1_Click(object sender, EventArgs e)
        {
            BindProducts();
            BindGridView();
           
        }
        private void BindProducts()
        {
            string a = "233Adsf 48872Hjyu 1289Rtop";
            string[] words = a.Split(' ');
           

            foreach (string word in words)
            {
                string myString = word;

                string alphabet = String.Empty;
                string digit = String.Empty;

                Match regexMatch = Regex.Match(myString, @"\D+");
                if (regexMatch.Success) //Found numeric part in the coverage string
                {
                    int digitStartIndex = regexMatch.Index; //Get the index where the numeric digit found
                    digit = myString.Substring(0, digitStartIndex);
                    alphabet = myString.Substring(digitStartIndex);
                                      

                    string sqlConnectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
                    SqlConnection con = new SqlConnection(sqlConnectionString);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into Test2 values('" + digit + "','" + alphabet + "')", con);

                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
          
        }
        private void BindGridView()
        {
            string constring = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
            
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Test2", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            dataGridView1.DataSource = dt;
                        }
                    }
                }
            }
            
        }