Error Message :-Update requires a valid InsertCommand when passed DataRow collection with new rows.

Joined
Apr 10, 2012
Messages
1
Reaction score
0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeTableManagementSystem
{
public partial class AddRecord : Form
{
SqlConnection cn;
SqlCommand cmd;
SqlDataAdapter ad;
SqlCommandBuilder cmdb;
DataSet ds;
public AddRecord()
{
InitializeComponent();
}

private void AddRecord_Load(object sender, EventArgs e)
{



cn = new SqlConnection();
cn.ConnectionString = Logic.GetConnectionString();
cmd = new SqlCommand("select * from faculty", cn);
ad = new SqlDataAdapter(cmd);
cmdb = new SqlCommandBuilder(ad);
ds = new DataSet();
ad.Fill(ds);
ds.Tables[0].Constraints.Add("pmk", ds.Tables[0].Columns[0], true);
if (ds.Tables[0].Rows.Count == 0)
textBox1.Text = "101";
else
{
string s = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][0].ToString();
textBox1.Text = (Convert.ToInt32(s) + 1).ToString();
}

}

bool IsValidSelected()
{

string sub1 = c2.SelectedItem.ToString();
string sub2 = c3.SelectedItem.ToString();
foreach (DataRow dr in ds.Tables[0].Rows)
{
if ((dr[3].ToString() == sub1 || dr[4].ToString() == sub1 || dr[3].ToString() == sub2 || dr[4].ToString() == sub2)&& dr[2].ToString()==c1.SelectedItem.ToString())
return false;
}
return true;
}

bool IsValidSelected1()
{

string sub1 = comboBox5.SelectedItem.ToString();
string sub2 = comboBox4.SelectedItem.ToString();
foreach (DataRow dr in ds.Tables[0].Rows)
{
if ((dr[7].ToString() == sub1 || dr[8].ToString() == sub1 || dr[7].ToString() == sub2 || dr[8].ToString() == sub2) && dr[6].ToString() == c1.SelectedItem.ToString())
return false;
}
return true;
}


private void button1_Click(object sender, EventArgs e)
{

DataRow dr = ds.Tables[0].NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;

dr[2] = c1.SelectedItem.ToString();
dr[3] = c2.SelectedItem.ToString();
dr[4] = c3.SelectedItem.ToString();
dr[5] = comboBox1.SelectedItem.ToString();
dr[6] = comboBox3.SelectedItem.ToString();
dr[7] = comboBox4.SelectedItem.ToString();
dr[8] = comboBox5.SelectedItem.ToString();
dr[9] = comboBox2.SelectedItem.ToString();


ds.Tables[0].Rows.Add(dr);
ad.Update(ds);
MessageBox.Show("Record is saved");
this.Close();

}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

DataTable dt = new DataTable();
ad = new SqlDataAdapter("select * from Syllabus where branch='" + c1.SelectedItem.ToString().Substring(0, 2) + "' and semester='" + comboBox1.SelectedItem.ToString() + "'", cn);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
string value = "";
c2.Items.Clear();
c3.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
value = dt.Rows["subjects"].ToString();
c2.Items.Add(value);
c3.Items.Add(value);
}
}

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
ad = new SqlDataAdapter("select * from Syllabus where branch='" + c1.SelectedItem.ToString().Substring(0, 2) + "' and semester='" + comboBox1.SelectedItem.ToString() + "'", cn);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
string value = "";
comboBox5.Items.Clear();
comboBox4.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
value = dt.Rows["subjects"].ToString();
comboBox5.Items.Add(value);
comboBox4.Items.Add(value);
}
}

}







}
}


form is as attachment pease help with this error
 

Attachments

  • Capture111.webp
    Capture111.webp
    11.3 KB · Views: 277
Back
Top