4
Answers

Solve The code problem

Vinit  Jain

Vinit Jain

11y
1.2k
1
i am making a windows form application to insert data. i used service based database for storing the data.
i am using following code.

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;
using System.Data.SqlClient;
using System.IO;

namespace App4
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True");
       
        SqlDataAdapter ad = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            ad.InsertCommand = new SqlCommand("insert into data (name,age,phone) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
            ad.InsertCommand.ExecuteNonQuery();
            ad.InsertCommand.Dispose();
            con.Close();
           

            ad.SelectCommand = new SqlCommand("select * from data", con);
            con.Open();
            ad.Fill(ds);
            con.Close();
        }
    }
}




in this code insert command executing and ds is also filling with the last entered data. but main problem is that this data is not showing into the data table.
why?????


please give the sollution for this problem.
Answers (4)
2
Amogh Natu
NA 2.2k 177.8k 11y
Correct me if I'm wrong.

I don't see any DataTable object instantiated here. However, if you want it to be filled into the data table, all you need to do is add these lines of code after the last con.Close();

DataTable dt = new DataTable();
dt = ds.Tables[0];

You can display the contents of the data table as well using MessageBox.

MessageBox.Show(Convert.ToString(dt.Rows[0][0]));
MessageBox.Show(Convert.ToString(dt.Rows[0][1]));
MessageBox.Show(Convert.ToString(dt.Rows[0][2]));

0
Pankaj Pandey
NA 6.3k 1.1m 11y
hiii..

try this to see the data...


DataTable dt=new DataTable();
dt=ds.Tables[0];

now try to show the data from datatable
0
Riddhi Valecha
NA 3.1k 186.7k 11y
Hi...

Can you share your project?? It will be easier to correct the code there...
0
Sandeep Singh Shekhawat
NA 22.4k 12m 11y
Hi Vinit,

You doing a minor mistake that is passing connection before opening connection see code in bold where i change line excution sequnce.
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;
using System.Data.SqlClient;
using System.IO;

namespace App4
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True");
        
        SqlDataAdapter ad = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            ad.InsertCommand = new SqlCommand("insert into data (name,age,phone) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
            ad.InsertCommand.ExecuteNonQuery();
            ad.InsertCommand.Dispose();
            con.Close();
            

//Connection should open before passing
                con.Open();
            ad.SelectCommand = new SqlCommand("select * from data", con);
            
            ad.Fill(ds);
            con.Close();
        }
    }
}