0

I have an following code in this I returned the dataset,but dont no how to use this dataset.Code is running,but not save the record in datase.
I had use same code vb.net,it works.
Plz help me...
====================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MyFdll;

namespace UseDll1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection cn;
        DataSet ds = new DataSet();
        Class1 c1 = new Class1();

        string cnstring;

        private void btnSave_Click(object sender, EventArgs e)
        {
            cnstring =  "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\Database\\MasterFile.mdb;
            

         ds =(DataSet) c1.get_ds(cnstring, "insert into Stud values('" + txtname.Text + "'," + txtrno + ",'" + txtadd.Text + "')", " ");
            //Program is Running but ds is null

            MessageBox.Show("Record Sucessfully inserted", "Message");

        }

   }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Data.SqlClient;


namespace MyFdll
{
    public class Class1
    {
        SqlConnection cn;
        DataSet dsm;
        SqlDataAdapter da1 ;

        public DataSet  get_ds(string cnstring,string oledbstr,string tb1)
        {
            cn = new SqlConnection(cnstring);
            dsm = new DataSet();
            try
            {
                cn.Open();
                da1 = new SqlDataAdapter(oledbstr,cn);
                if(tb1 != "")
                {
                    da1.Fill(dsm,tb1);

                }
                else
                {
                    da1.Fill(dsm);
                }
                return dsm;
            }

                catch (Exception ex)
            {
                    Console.WriteLine(ex.Message.ToString());
                }
            finally
            {
                cn.Close();
            }
            return dsm;
            }
 
        }

    }

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

>dataset.Code is running,but not save the record in datase.
I had use same code vb.net,it works.

Your code has big problem. First of all, use System.Data.Oledb namespace classes with Ms-Access database.

No need to use Dataset in your code to save/insert a row.

OleDbConnection cn=new OleDbConnection(cnstring);
OleDbCommand cmd=new OleDbCommand("INSERT INTO STUD (RNO,NAME,ADD) VALUES (@rno,@name,@add)",cn);

cmd.Parameters.AddWithValue("@rno",txtrno.Text);
cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@add",txtadd.Text);

cn.Open();
cmd.ExecuteNonQuery();
cn.Close()
....
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.