Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends, I have small issue , It may be a silly question but i cant understand why this happens?

I made simple winapp and in form I have 1 datagridview which shows a datatable in it and this dgv takes value from db and db gets values for imported files. this is the scenario of my task.

Now when I run project and click button for files importing files get imported and showing data in dgv but one column not shows value in dgv, it displays value in database but when I excute import method it not gets value. help me please. I am stuck here.

this is file import method:

C#
public void ImportAllFilesOfFolder()
{
    try
    {
        DataTable dt = new DataTable();
        string sourceDir = txtsend.Text;
        var Icsv = Directory.EnumerateFiles(sourceDir, "*.csv");
        bool firstfile = true;

        foreach (string currentfile in Icsv)
        {
            using (TextFieldParser parser = new TextFieldParser(currentfile))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.Delimiters = new string[] { "," };
                parser.HasFieldsEnclosedInQuotes = true;

                string[] columns = parser.ReadFields();

                if (firstfile == true)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        dt.Columns.Add(columns[i], typeof(string));
                    }
                }

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    DataRow newrow = dt.NewRow();

                    for (int i = 0; i < fields.Length; i++)
                    {
                        newrow[i] = fields[i];
                    }

                    dt.Rows.Add(newrow);
                }
            }
            firstfile = false;
        }
        con.Open();
        SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
        bc.DestinationTableName = "PatientTable";
        bc.BatchSize = dt.Rows.Count;
        bc.WriteToServer(dt);
        bc.Close();
    }
    catch { }//(Exception e) { MessageBox.Show(e.Message); }
    finally { con.Close(); }
}

this is set data method to save record in database and display in dgv

C#
public void SetData()
{
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * from PatientTable", con);
    //DataSet ds = new DataSet();
    da.Fill(saiNathHospitalDataSet, "PatientTable");
    dataGridView1.DataSource = saiNathHospitalDataSet.Tables["PatientTable"];
    con.Close();
}
Posted
Updated 3-Jun-15 21:54pm
v2
Comments
ZurdoDev 4-Jun-15 9:29am    
We can't see your data or run your code so you'll need to debug it and see why one column is empty.
Vyshakhjs 4-Jun-15 11:04am    
Make sure that column index no on loop matching the datagrid column position Column index starts from 0
Member 11543226 5-Jun-15 1:28am    
I checked all values binding but in dgv one column values not showing of "BED NO".

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900