2
Answers

Windows application sql problem

Arunkumar Emm

Arunkumar Emm

13y
1.1k
1
Hi ,
 
Lately i was trying to develop a windows application with c# n sql 2005. i got stucked at a point where i can insert data into the sql db but when i close n open my application, the data is gone. i'll explain briefly,
i've created the sql database(i.e, SKTDB.mdf) and table using server explorer in visual studio 08. my connection string is like ,

sDBPath = Application.StartupPath + "\\DB\\SKTDB.mdf";
 if (System.IO.File.Exists(sDBPath))
 {
   connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + sDBPath + "';User ID=sa;Password=1234";
 }

 
I have a registration form in my app. when i fill the form and submit , the data is inserted into the table in SKTDB.mdf. but when i restarts my application , the table is empty. I cant figure out the reason. if u guys can , pls let me know..
Answers (2)
0
Hemant Kumar

Hemant Kumar

NA 3k 215.1k 13y
If your column type is of number type(such as double), the formating would do the work no matter in edit mode or not.
If your column type is of string type which is by default, you shoud handle the cellFormating event to do a trick work like below:

Code Snippet

private void DgvCellStyle_Load(object sender, EventArgs e)

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("a");

            dt.Rows.Add(155.6565);

            dt.Rows.Add(2342.2);

            this.dataGridView1.DataSource = dt;

 

            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

        }

 

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

        {

            if (e.ColumnIndex == 0 && e.RowIndex != this.dataGridView1.NewRowIndex)

            {

                double d = double.Parse(e.Value.ToString());

                e.Value = d.ToString("N2");

            }

        }