如何将gridview连接到数据库

时间:2013-11-09 09:53:39

标签: c# asp.net gridview database-connection

<asp:GridView ID="grvStudentDetails" runat="server" 
            ShowFooter="True" AutoGenerateColumns="False"
            CellPadding="4"  
            GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting">
<Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="" />
    <asp:TemplateField HeaderText="From">
         <ItemTemplate>
            <asp:DropDownList ID="drpQualification" runat="server" >
                <asp:ListItem Value="G">kolkata</asp:ListItem>

                <asp:ListItem Value="P">Mumbai</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="To">
        <ItemTemplate>
           <asp:DropDownList ID="drQualification" runat="server">
                <asp:ListItem Value="G">Bangalore</asp:ListItem>
                <asp:ListItem Value="P">Delhi</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Travel Date">
        <ItemTemplate>
              <asp:TextBox ID="txtAddress" runat="server" ></asp:TextBox>

        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="">
        <ItemTemplate>

        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="">
        <ItemTemplate>

        </ItemTemplate>
        <FooterStyle HorizontalAlign="Right" />
        <FooterTemplate>
            <asp:Button ID="ButtonAdd" runat="server" 
                    Text="Add New City" OnClick="ButtonAdd_Click" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowDeleteButton="True" />
</Columns>

</asp:GridView>
  

我在我的asp页面中有以下gridview,现在我想连接所有内容意味着从这个gridview到我的数据库点击保存按钮,我将如何做到这一点,什么所有我必须在我的.cs文件中进行.PLZZZ帮助

protected void Button1_Click(object sender, EventArgs e)
{
    string insrt = "insert into book_mas_det(book_date,book_from,book_to,travel_date,return_date,adult_pax,child_pax,travel_class,contact_name,pnr_no,tot_amt,adv_amount,travler_rmks,agent_rmks) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n)";
    MySqlCommand cmd = new MySqlCommand(insrt, conn);
    cmd.Parameters.AddWithValue("@a", txtbookingdate.Text);
    cmd.Parameters.AddWithValue("@b", ddlfrom.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@c", ddlto.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@d", txtdeprtr.Text);
    cmd.Parameters.AddWithValue("@e", txtreturn.Text);
    cmd.Parameters.AddWithValue("@f", txtadult.Text);
    cmd.Parameters.AddWithValue("@g", txtchild.Text);
    cmd.Parameters.AddWithValue("@h", ddlclass.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@i", txtcontpername.Text);
    cmd.Parameters.AddWithValue("@j", txtcontmobile.Text);
    cmd.Parameters.AddWithValue("@k", txtamnttocharge.Text);
    cmd.Parameters.AddWithValue("@l", txtadvamnt.Text);
    cmd.Parameters.AddWithValue("@m", txtcmnttrav.Text);
    cmd.Parameters.AddWithValue("@n", txtcmntagnt.Text);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    string display = "Booking Details is added successfully";
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
  

我为view1连接添加了我的.cs代码,现在如何为view2和view3添加连接

3 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助.. !!这是我的代码.. !!你可以替换你的模板字段texbox ID .. !!我想你知道如何将字符串值保存到数据库中的表.. !!我认为这段代码对你来说已经足够了。!!我想告诉你.. !!

 try
    {
        for (int i = 0; i < gvInvTransaction.Rows.Count - 1; i++)
        {
            TextBox txtproduct = (TextBox)gvInvTransaction.Rows[i].FindControl("txtProduct");
            TextBox txtBarcode = (TextBox)gvInvTransaction.Rows[i].FindControl("txtBarCode");
            TextBox txtPrdctBatchID = (TextBox)gvInvTransaction.Rows[i].FindControl("txtPrdctBatchID");
            TextBox txtPrdctID = (TextBox)gvInvTransaction.Rows[i].FindControl("txtPrdctID");
            TextBox txtStdPurchasePrice = (TextBox)gvInvTransaction.Rows[i].FindControl("txtUnitPrice");
            TextBox txtStdSalesPrice = (TextBox)gvInvTransaction.Rows[i].FindControl("txtUnitPrice");





            string  ProductID = Convert.ToInt64(txtPrdctID.Text);
            string  StdPurchasePrice = Convert.ToDouble(txtStdPurchasePrice.Text);
            string  StdSalesPrice = Convert.ToDouble(txtStdPurchasePrice.Text);







        }
    }
    catch (Exception ex) { }
}

答案 1 :(得分:0)

SqlConnection con=new SqlConnection("Your Connection String");
SqlCommand cmd=new SqlCommand("Insert into "Your table" "Your Values"");
cmd.executeNonquery();
DataTable dt=new DataTable();
DataAdapter da=new DataAdapter(cmd);
da.Fill(dt);
GridView1.Datasource=dt;
Gridview1.databind();

可能是这些ADO.Net将有助于在数据库中存储Gridview值。 它应该在button_click事件中完成,以便您的值将存储在数据库中。

答案 2 :(得分:-1)

只需为其他2个数据库插入并添加参数创建另一个字符串和Sql命令对象,在参数中传递从gridview获取的字符串值,然后调用cmd2.ExecuteNonQuery();和cmd2.execute .. !!以下只是一个示例替换它与您的每个视图值.. !!希望能帮助到你..!! :)

 protected void Button1_Click(object sender, EventArgs e)
{
    string insrt = "insert into book_mas_det(book_date,book_from,book_to,travel_date,return_date,adult_pax,child_pax,travel_class,contact_name,pnr_no,tot_amt,adv_amount,travler_rmks,agent_rmks) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n)";
    string insrt2 = "insert into book_mas_det(book_date,book_from,book_to,travel_date,return_date,adult_pax,child_pax,travel_class,contact_name,pnr_no,tot_amt,adv_amount,travler_rmks,agent_rmks) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n)";
    string insrt3 = "insert into book_mas_det(book_date,book_from,book_to,travel_date,return_date,adult_pax,child_pax,travel_class,contact_name,pnr_no,tot_amt,adv_amount,travler_rmks,agent_rmks) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n)";

    MySqlCommand cmd = new MySqlCommand(insrt, conn);
    MySqlCommand cmd2 = new MySqlCommand(insrt2, conn);
    MySqlCommand cmd3 = new MySqlCommand(insrt3, conn);

    //cmd parameters
    cmd.Parameters.AddWithValue("@a", txtbookingdate.Text);
    cmd.Parameters.AddWithValue("@b", ddlfrom.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@c", ddlto.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@d", txtdeprtr.Text);
    cmd.Parameters.AddWithValue("@e", txtreturn.Text);
    cmd.Parameters.AddWithValue("@f", txtadult.Text);
    cmd.Parameters.AddWithValue("@g", txtchild.Text);
    cmd.Parameters.AddWithValue("@h", ddlclass.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@i", txtcontpername.Text);
    cmd.Parameters.AddWithValue("@j", txtcontmobile.Text);
    cmd.Parameters.AddWithValue("@k", txtamnttocharge.Text);
    cmd.Parameters.AddWithValue("@l", txtadvamnt.Text);
    cmd.Parameters.AddWithValue("@m", txtcmnttrav.Text);
    cmd.Parameters.AddWithValue("@n", txtcmntagnt.Text);

   //cmd2 parameters -replace this with your second view values 
    cmd2.Parameters.AddWithValue("@a", txtbookingdate.Text);
    cmd2.Parameters.AddWithValue("@b", ddlfrom.SelectedItem.Text);
    cmd2.Parameters.AddWithValue("@c", ddlto.SelectedItem.Text);
    cmd2.Parameters.AddWithValue("@d", txtdeprtr.Text);
    cmd2.Parameters.AddWithValue("@e", txtreturn.Text);
    cmd2.Parameters.AddWithValue("@f", txtadult.Text);
    cmd2.Parameters.AddWithValue("@g", txtchild.Text);
    cmd2.Parameters.AddWithValue("@h", ddlclass.SelectedItem.Text);
    cmd2.Parameters.AddWithValue("@i", txtcontpername.Text);
    cmd2.Parameters.AddWithValue("@j", txtcontmobile.Text);
    cmd2.Parameters.AddWithValue("@k", txtamnttocharge.Text);
    cmd2.Parameters.AddWithValue("@l", txtadvamnt.Text);
    cmd2.Parameters.AddWithValue("@m", txtcmnttrav.Text);
    cmd2.Parameters.AddWithValue("@n", txtcmntagnt.Text);

   //cmd3 parameters- replace this with your third view values 
    cmd3.Parameters.AddWithValue("@a", txtbookingdate.Text);
    cmd3.Parameters.AddWithValue("@b", ddlfrom.SelectedItem.Text);
    cmd3.Parameters.AddWithValue("@c", ddlto.SelectedItem.Text);
    cmd3.Parameters.AddWithValue("@d", txtdeprtr.Text);
    cmd3.Parameters.AddWithValue("@e", txtreturn.Text);
    cmd3.Parameters.AddWithValue("@f", txtadult.Text);
    cmd3.Parameters.AddWithValue("@g", txtchild.Text);
    cmd3.Parameters.AddWithValue("@h", ddlclass.SelectedItem.Text);
    cmd3.Parameters.AddWithValue("@i", txtcontpername.Text);
    cmd3.Parameters.AddWithValue("@j", txtcontmobile.Text);
    cmd3.Parameters.AddWithValue("@k", txtamnttocharge.Text);
    cmd3.Parameters.AddWithValue("@l", txtadvamnt.Text);
    cmd3.Parameters.AddWithValue("@m", txtcmnttrav.Text);
    cmd3.Parameters.AddWithValue("@n", txtcmntagnt.Text);




    conn.Open();
    cmd.ExecuteNonQuery();
     cmd2.ExecuteNonQuery();
    cmd3.ExecuteNonQuery();

    conn.Close();
    string display = "Booking Details is added successfully";
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);