Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use SqlCommand and DataAdapter in order to manipulate SQL Server with C#
    primarykey
    data
    text
    <ol> <li><p>I create table called <code>Department</code> with 2 columns <code>Department ID</code> which is auto increment and <code>Department Name</code></p></li> <li><p>I create <code>Navigate_Department()</code> in order to walk through the department rows</p> <pre><code>System.Data.SqlClient.SqlConnection con; DataSet Dep_ds; System.Data.SqlClient.SqlDataAdapter Dep_da; int Dep_MaxRows = 0; int Dep_inc = 0; private void ILS_Load(object sender, EventArgs e) { con = new System.Data.SqlClient.SqlConnection(); con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ILS_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; con.Open(); Dep_ds = new DataSet(); string sql2 = "select * from Department order by DepartmentID"; Dep_da = new System.Data.SqlClient.SqlDataAdapter(sql2, con); Dep_da.Fill(Dep_ds, "Department"); Navigate_Department(); Dep_MaxRows = Dep_ds.Tables["Department"].Rows.Count; } private void Navigate_Department() { DataRow dRow = Dep_ds.Tables["Department"].Rows[Dep_inc]; Dep_ID.Text =dRow.ItemArray.GetValue(0).ToString(); Dep_Name.Text = dRow.ItemArray.GetValue(1).ToString(); } private void Move_Next_Click(object sender, EventArgs e) { if (Dep_inc != Dep_MaxRows-1) { Dep_inc++; Navigate_Department(); } else { MessageBox.Show("No More Records"); } } private void Move_back_Click(object sender, EventArgs e) { if (Dep_inc &gt; 0) { Dep_inc--; Navigate_Department(); } else { MessageBox.Show("First Record"); } } private void Dep_Clear_Click(object sender, EventArgs e) { Dep_ID.Clear(); Dep_Name.Clear(); } private void Dep_Add_Click(object sender, EventArgs e) { try { SqlCommand insCmd = new SqlCommand("insert into dbo.Department (DepartmentName) values ('" + Dep_Name.Text + "')", con); Dep_da.InsertCommand = insCmd; Dep_MaxRows = Dep_MaxRows + 1; Dep_inc = Dep_MaxRows - 1; Dep_Max.Text = Dep_MaxRows.ToString(); Dep_Current.Text = (Dep_MaxRows).ToString(); } catch (Exception exceptionObject) { MessageBox.Show(exceptionObject.Message); } </code></pre></li> </ol> <p>The problem is:</p> <p>After I click clear button, I insert the department name into <code>Dep_Name</code> textbox then click add button. The name that I inserted didn’t get saved in the database, and if I click move back then move next in order to see what I inserted, I get a <code>Index out of range</code> exception in the <code>Navigate_Department()</code> method.</p> <p>So did I make any mistake?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload