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:
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 { }
finally { con.Close(); }
}
this is set data method to save record in database and display in dgv
public void SetData()
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * from PatientTable", con);
da.Fill(saiNathHospitalDataSet, "PatientTable");
dataGridView1.DataSource = saiNathHospitalDataSet.Tables["PatientTable"];
con.Close();
}