0
<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 进行连接

4

3 回答 3

0

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

 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) { }
}
于 2013-11-09T10:14:23.137 回答
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 事件中完成,以便您的值将存储在数据库中。

于 2013-11-09T11:04:37.057 回答
-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);
于 2013-11-09T17:48:51.043 回答