Firstly thanks to SanjeevSingh after putting in this line I was able to get it to work.
The I would like to thank Wayne Gaylard for bringing it all together so well.
This is what it looks like :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace PetStore
{
public partial class _Default : System.Web.UI.Page
{
string DBConn;
protected void Page_Load(object sender, EventArgs e)
{
DBConn = ConfigurationManager.ConnectionStrings["PetstoreDBConnectionString"].ConnectionString;
}
int InsertProduct()
{
using (SqlConnection myConnection = new SqlConnection(DBConn))
{
SqlCommand MyCommand = new SqlCommand("INSERT INTO TheTableName (Column1, Column2, Column3) Values (@Column1, @Column2, @Column3)", myConnection);
MyCommand.Parameters.AddWithValue("@Column1", TextBox1.Text);
MyCommand.Parameters.AddWithValue("@Column2", TextBox2.Text);
MyCommand.Parameters.AddWithValue("@Column3", TextBox3.Text);
myConnection.Open();
return MyCommand.ExecuteNonQuery();
}
}
then simply added the following to the button_click event:
protected void btnInsert_Click(object sender, EventArgs e)
{
InsertProduct();
}
Now what I want to do is display the table in the database, this is what I have tried:
int DisplayTable()
{
using (SqlConnection myConnection2 = new SqlConnection(DBConn))
{
SqlCommand MyCommand2 = new SqlCommand("SELECT * from TheTableName;", myConnection2);
myConnection2.Open();
SqlDataReader reader = MyCommand2.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
return MyCommand2.ExecuteNonQuery();
}
}
Then put in the button like I did before:
protected void btnDisplay_Click(object sender, EventArgs e)
{
DisplayTable();
}
but this does not work and gives the eorr:
"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."
What should I do?