simflex says:
sql += "Insert into Speakers (SpeakerID, SpeakerName,ClientName,Email,dateAdded, WebsiteURL,MinistryName) " ;
sql += "SELECT SpeakerID, SpeakerName,ClientName,Email,dateAdded,WebsiteURL,MinistryName from SpeakerHistory " ;
sql += " where SpeakerID = " + grvSpeakerBin.DataKeys[e.RowIndex].Values[1].ToString() + " " ;
cnn = new SqlConnection(strConnStr);
cnn.Open();
cmddel = new SqlCommand(sql, cnn);
cmddel.ExecuteNonQuery();
cmddel.Dispose();
cnn.Close();
this .BindSpeakersGrid();
}
|
Remove SpeakerID column from query, if it is IDENTITY column in the table.
Check with below code.
sql += "Insert into Speakers (SpeakerName,ClientName,Email,dateAdded, WebsiteURL,MinistryName) ";
sql += "SELECT SpeakerName,ClientName,Email,dateAdded,WebsiteURL,MinistryName from SpeakerHistory ";
sql += "where SpeakerID = '" + grvSpeakerBin.DataKeys[e.RowIndex].Values[1].ToString() + "'";
cnn = new SqlConnection(strConnStr);
cnn.Open();
cmddel = new SqlCommand(sql, cnn);
cmddel.ExecuteNonQuery();
cmddel.Dispose();
cnn.Close();
this.BindSpeakersGrid();
}
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<hr />
<asp:TextBox runat="server" ID="txtId" />
<asp:Button Text="Add" runat="server" OnClick="OnAdd" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
protected void OnAdd(object sender, EventArgs e)
{
String strConnStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "";
sql += "INSERT INTO Customers (Name,Country) ";
sql += "SELECT Name,Country FROM Customers ";
sql += "WHERE CustomerId = '" + txtId.Text.Trim() + "'";
SqlConnection con = new SqlConnection(strConnStr);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = ""
sql += "INSERT INTO Customers (Name,Country) "
sql += "SELECT Name,Country FROM Customers "
sql += "WHERE CustomerId = '" & txtId.Text.Trim() & "'"
Dim con As SqlConnection = New SqlConnection(strConnStr)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(sql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Sub
Screenshot
