将数据插入drisview并在同一gridview中显示

时间:2018-10-12 09:01:47

标签: c# sql asp.net sql-server

这既不会产生错误,也不会输出。帮我解决这个问题。这是我的代码:

public partial class ScoHelpDesk : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
    SqlDataAdapter adapt;
    DataTable dt;
    SqlConnection con2, con3;
    SqlCommand cmd, cmd1, cmd2;


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
          BindData_Sheet();
            }

    }
    protected void InsertData1(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlserver"].ConnectionString);

                TextBox t1 = (TextBox)GridView1.FooterRow.FindControl("TextBox2");
                TextBox t2 = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
                TextBox t3 = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
                TextBox t4 = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
                TextBox t5 = (TextBox)GridView1.FooterRow.FindControl("TextBox10");
                TextBox t6 = (TextBox)GridView1.FooterRow.FindControl("TextBox12");

                cmd = new SqlCommand("insert into tblHelpDesk (Name,Purpose,ContactNo,AlternativeNo,Email,Address) values(@Name,@Purpose,@ContactNo,@AlternativeNo,@Email,@Address)", con);

                con.Open();

                cmd.Parameters.AddWithValue("@Name", t1.Text);
                cmd.Parameters.AddWithValue("@Purpose", t2.Text);
                cmd.Parameters.AddWithValue("@ContactNo", t3.Text);
                cmd.Parameters.AddWithValue("@AlternativeNo", t4.Text);
                cmd.Parameters.AddWithValue("@Email", t5.Text);
                cmd.Parameters.AddWithValue("@Address", t6.Text);

                cmd.ExecuteNonQuery();
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }
        }
        catch (Exception ex)
        { }
    }
    private void BindData_Sheet()
    {

        DataTable dt = new DataTable();
        using (SqlConnection con2 = new SqlConnection(con.ConnectionString))
        {

            string strQuery = "SELECT * FROM tblHelpDesk ";
            SqlCommand cmd = new SqlCommand(strQuery, con);
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
}

0 个答案:

没有答案