i am having an issue in inserting record in gridview and show it when application runs. i am using executenonquery() method .my code is below.
protected void Page_Load(object sender, EventArgs e)
{
string select = "select * from student_table";
string cs = ConfigurationManager.ConnectionStrings["Database1ConnectionString1"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(cs);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into student_table (id,name,class) values(1,'danish qamar',6)"+select ,conn);
cmd.CommandType = CommandType.Text;
GridView1.DataSource = cmd.ExecuteNonQuery();
GridView1.DataBind();
conn.Close();
}
by using above code record is inserting in the database but not displaying when application run
i got following error:
Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.
help me to solve it please.
Loading
Francis SusaimichaelPosted Feb 8, 2015, 3:48 AM
umair mohsinPosted Feb 8, 2015, 2:28 AM
i understand your code but haven't apply yet.could you please explain what your code exactly says.thank you
umair mohsinPosted Feb 8, 2015, 2:24 AM
as i mentioned in post that i want to insert data in a database by using executenonquery().
record(data) is inserted in db but application does not run giving the error i mentioned. i want my application to insert data and display it on runtime.you can have my code in the post.
Francis SusaimichaelPosted Feb 8, 2015, 2:09 AM
protected void Page_Load(object sender, EventArgs e)
{
string select = "select * from student_table";
string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString.ToString();
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into student_table(id,name,age) values(10,'danish qamar',6)" , conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
DataTable dt=new DataTable("MyTable");
//rebind the datagrid
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Abhijit PatilPosted Feb 8, 2015, 1:33 AM
Can you tell me what exzactly what is your requirment.coz the code is not understanable which exazactly you write and write .so please elobrate your requirment .and i provide the exzact code .
Hope you understand