none
C#:Data not appearing in datagridview RRS feed

  • Question

  • I have made a multi form application. The application connects to a database trough linq to sql.

    On one of the forms there is a datagridview. So I input some data into the data grid view, I press the insert button and it says that the data is saved and no error messages appear. I also have a delete button there. So I input some values, I delete some(without error messages), then I input some values again. However when I close the form and load the same form again, the datagridview is empty.
    How do I check if my connectionstring works?

    How can we solve that problem? The code in question is here:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace AplikacijaGUI
    {
        
        public partial class Adminglumci : Form
        {
            public Adminglumci()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var confirmResult = MessageBox.Show("Are you sure you want to save ??",
                                         "Save?",
                                         MessageBoxButtons.YesNo);
                if (confirmResult == DialogResult.Yes)
                {
                    saveglumac();
    
                    // If 'Yes', do something here.
                }
                else
                {
                    // If 'No', do something here.
                }
            }
            private void saveglumac()
            {
                DataClasses1DataContext linqglu = new DataClasses1DataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AplikacijaBase.mdf;Integrated Security=True;Connect Timeout=30");           
                int rowindex = dataGridView1.CurrentRow.Index; // here rowindex will get through currentrow property of datagridview.
                Glumci GI1 = new Glumci();
                GI1.ID = Convert.ToInt32(dataGridView1.Rows[rowindex].Cells[0].Value);
                GI1.ime = Convert.ToString(dataGridView1.Rows[rowindex].Cells[1].Value);
                GI1.prezime = Convert.ToString(dataGridView1.Rows[rowindex].Cells[2].Value);
                linqglu.Glumcis.InsertOnSubmit(GI1);
                linqglu.SubmitChanges();
                MessageBox.Show("Spremljeno");
                rowindex = 0;
            }
            private void delglumac()
            {
                DataClasses1DataContext linqglu = new DataClasses1DataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AplikacijaBase.mdf;Integrated Security=True;Connect Timeout=30");
                int rowindex = dataGridView1.CurrentRow.Index; // here rowindex will get through currentrow property of datagridview.
                Glumci GI2 = new Glumci();
                int iid = 0;
                iid = Convert.ToInt32(dataGridView1.Rows[rowindex].Cells[0].Value);
                var delete = from p in linqglu.Glumcis
                             where p.ID == iid// match the ecords.
                             select p;
                linqglu.Glumcis.DeleteAllOnSubmit(delete);// DeleteAllOnSubmit function will call and queries will automatic call thats the data context class handle it.
                linqglu.SubmitChanges();
                // SI = SDCD1.StudentInfos.Single(c => c.Id == iid);
                rowindex = 0;
                MessageBox.Show("deleted");
                Refresh();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                var confirmResult = MessageBox.Show("Are you sure you want to save ??",
                                         "Save?",
                                         MessageBoxButtons.YesNo);
                if (confirmResult == DialogResult.Yes)
                {
                    delglumac();
    
                    // If 'Yes', do something here.
                }
                else
                {
                    // If 'No', do something here.
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                InsertGlumci iglumci = new InsertGlumci();
                iglumci.Show();
            }
        }
    
    }



    • Edited by mkam77 Thursday, August 27, 2015 8:13 PM
    • Moved by Kristin Xie Friday, August 28, 2015 3:05 AM move to appropriate forum
    Thursday, August 27, 2015 8:00 PM

All replies

  • Hi mkam,

    Based on your code, I see your create instance of Data Context class,  your case more related  LINQ to SQL to select, and delete data and so on. So I moved your case to Data Platform Development >LINQ to SQL forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 28, 2015 3:09 AM
  • Hi mkam77,

    what is DataClasses1DataContext ? Is it customized?

    --> How do I check if my connectionstring works?

    You can let the SqlConnectionStringBuilder constructor check it:

    bool isValidConnectionString = true;
    
    try{
    
        var con = new SqlConnectionStringBuilder("ABC");
    
    }catch(Exception)
    
    {
    
        // can be KeyNotFoundException, FormatException, ArgumentException
    
        isValidConnectionString = false;
    
    }
    

    If isValidConnetionString is true, the connectionstring could work.

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 9, 2015 2:51 AM