SQL ---> dodawanie nowego rekordu..klopot

0

Witam,

Podczas proby stworzenia rekordu napotkałem na dziwny bład i nie wiem co mam z tym zrobić mianowicie w zaznaczonych liniach pojawia się bład podczas proby dodania rekordu " Object reference not set to an instance of an object."

Proszę o pomoc bo nie wiem jak sobie z tym poradzic.

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {   System.Data.SqlClient.SqlConnection con;
        DataSet ds1;
        System.Data.SqlClient.SqlDataAdapter da;
        public Form1()
        {
            InitializeComponent();
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            int MaxRows = 0;
            int inc = 0;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "Data Source=STUDENT-B8BB779\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True";
            con.Open();
            ds1 = new DataSet();
            string sql = "SELECT * From tblUser";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            <b>MaxRows = ds1.Tables["tblUser"].Rows.Count; </b>
           <b> DataRow dRow = ds1.Tables["tblUser"].NewRow();</b>
            dRow[1] = textBox1.Text;
            ds1.Tables["tblUser"].Rows.Add(dRow);
            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;
            da.Update(ds1, "tblUser");
        }
    }
}

baza wyglada tak :

baza nazywa sie Database1.mdf , w niej jest tabela tblUser a w niej sa 4 pola login , password , ID , time.

Z gory dziękuje za pomoc

0

Błąd się pojawia bo do swojego DataSet nie dodałeś żadnych tabel.

0

hmm no racja, ale jak dodac tabele do tego teraz ?
bo jak daje ds1 = new DataSet("tblUser")
; to dalej nie smiga , a obecnie brak mi pomyslu jak zrobic to inaczej :(

0

Dodałem nową tabele ale przy update teraz mam problem gdyż wywala mi :
"Update unable to find TableMapping['login'] or DataTable 'login'."

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {   System.Data.SqlClient.SqlConnection con;
        DataSet ds1;
        System.Data.SqlClient.SqlDataAdapter da;
        public Form1()
        {
            InitializeComponent();
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
         
            int MaxRows = 0;
            int inc = 0;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "Data Source=STUDENT-B8BB779\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True";
            con.Open();
            ds1 = new DataSet("tblUser");

            string sql = "SELECT * From tblUser";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            DataTable newTable = new DataTable("tblUser");
            newTable.Columns.Add("login", typeof(string));
           // newTable.Columns.Add("password", typeof(string));
            ds1.Tables.Add(newTable);
            MaxRows = ds1.Tables["tblUser"].Rows.Count;
            DataRow dRow = ds1.Tables["tblUser"].NewRow();
            dRow[0] = textBox1.Text;
            ds1.Tables["tblUser"].Rows.Add(dRow);
            MaxRows = MaxRows + 1;
            da.Update(ds1, "login"); //  <---------  tu blad 
            con.Close();

        }
    }
}
0

no i dobrze Ci wywala blad bo tabela u Ciebie nazywa sie tblUser a nie login

0

Po zmienie wywala mi :
Update requires a valid InsertCommand when passed DataRow collection with new rows.

0

może w ten sposób:

SqlConnection conn = new SqlConnection("...");
SqlDataAdapter da = new SqlDataAdapter(conn);
SqlCommand cmdInsert = new SqlCommand("insert into .... values ....", conn);
SqlCommand cmdSelect = new SqlCommand("select ... from ...", conn);
da.SelectCommand = cmdSelect;
da.InsertCommand = cmdInsert;
0

To nie działa jak jak myślisz - nie używaj obiektów DataAdapter/DataTable/DataSet to takich rzeczy.

Jeśli chcesz dodać jeden wiersz, starczy ci SqlCommand i ExecuteNonQuery();

1 użytkowników online, w tym zalogowanych: 0, gości: 1