0

I am using SMS caster to send sms.It has an option to Import csv files.

Now I want to dynamically create csv file of CellNo column of Person table from Visual Studio 2010 connected SQL Server 2008.So that I click on a button and it creates a csv file which I can then access from my software SMSCaster to send sms.

The solutions available are either manual-based or if some query is provided it requires Microsoft OLEDB.....so is there any simple query to convert queryresult into .csv file?

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Muhammad Suleman
  • 53
  • 1
  • 1
  • 6

2 Answers2

2

Try this :

Namespace : System.IO;

        var _lines = new List<string>();
        for(int _i=0;i<gridview1.rows.count;_i++)
        {
            string[] _mobileNos = gridView1.rows[_i].cells[mobilecolumn index in gridview].text;
            var header = string.Join(",", _mobileNos);
            _lines.Add(header);
        }
        File.WriteAllLines("FileName.csv",_lines);
Revan
  • 1,104
  • 12
  • 27
  • @Muhammad Suleman example : if your mobile no is in 3rd column.. use gridView1.rows[_i].cells[3].text – Revan Feb 21 '14 at 09:47
  • One thing remaining..."gvCellNo.Rows[_i].Cells[0].Value"....gives an error, "cannot convert object type to string[]"....also "gvCellNo.Rows[_i].Cells[0].Text" too doesn't work. – Muhammad Suleman Feb 21 '14 at 10:44
  • public void gridtoCSV() { var _lines = new List(); for (int _i = 0; _i < gvCellNo.Rows.Count; _i++) { string[] _mobileNos = gvCellNo.Rows[_i].Cells[0].Value; var header = string.Join(",", _mobileNos); _lines.Add(header); } File.WriteAllLines("FileName.csv", _lines); } – Muhammad Suleman Feb 21 '14 at 10:49
  • @Mohammad Suleman .. string[] _mobileNos ; In for loop _mobileNos[_i]=gvCellNo.Rows[_i].Cells[0].Value – Revan Feb 21 '14 at 10:52
  • what is myvalue and ing – Revan Feb 21 '14 at 11:24
  • So @Revanayya Hiremath...finally this code worked public void gridtoCSVFILE() { string ing; List lines = new List(); for (int i = 0; i < gvCellNo.Rows.Count; i++) { ing = gvCellNo.Rows[i].Cells[0].Value.ToString(); lines.Add(ing); } File.WriteAllLines("FileName.csv", lines); } Thanks very very much mate...One thing more...I want to replace old .csv file with old one each time it is created...any suggestion?? – Muhammad Suleman Feb 21 '14 at 11:35
  • @Muhammad Suleman Check the existence of file like if(File.exists("Filepath")) – Revan Feb 21 '14 at 11:52
  • ....After creating setup...the respective csv file that is made in bin folder before setup is not made.Can u help?Bcz after Installer Setup there is only Release and Debug folder – Muhammad Suleman Feb 25 '14 at 06:00
  • @Muhammad Suleman Can I put one more answer or shall i comment here ? – Revan Feb 25 '14 at 06:04
  • ver very sorry..I actually don't know where to click..plz tell me...I "ll first do it...rest afterwards...I tried to voteup several times..but it says I require 15 reputations for this – Muhammad Suleman Feb 25 '14 at 06:46
  • Done mate...So csv file after setup – Muhammad Suleman Feb 25 '14 at 07:12
  • Before creating the setup.. instead of build.. make it as Release and build application and make setup – Revan Feb 25 '14 at 07:16
  • could u plz tell me how to make it as Release,as u told...bcz csv file is not being made. – Muhammad Suleman Feb 26 '14 at 12:44
  • Yes sure..Use File.WriteAllLines("PathName\FileName.csv",_lines); or infront of Run button select Release from dropdown list – Revan Feb 26 '14 at 13:02
0

Here is the solution that worked:

public void gridtoCSVFILE()

    {
        string ing;
        List<string> lines = new List<string>();
        for (int i = 0; i < gvStudCellNo.Rows.Count; i++)
        {
            ing = gvStudCellNo.Rows[i].Cells[0].Value.ToString();
            lines.Add(ing);
            File.WriteAllLines("StudentsCellNo.csv", lines);
        }
    }

//it will create csv file in your bin folder...also it automatically replaces each new file with the old one

Muhammad Suleman
  • 53
  • 1
  • 1
  • 6