2013-11-09 48 views
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,現在我想所有的內容連接是指,到日期etc..of這個GridView控件到我的數據庫上的點擊保存按鈕,我怎麼會做這個,file.PLZZZ幫助什麼都我在我的.cs做如何GridView控件連接到數據庫

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); 

我已經加了我的.cs代碼,廠景的連接,現在該怎麼辦view2和view3的連通性

+0

= grid.DataSource數據源; grid.Bind() –

+0

@DoobyInc:在哪個funtcion我必須使用它? – Naveen31

+0

在後面的代碼,處理OnBeforePageLoad事件,我不知道事件名稱,但你可以在谷歌搜索,有許多例子avaialbe那裏 –

回答

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) { } 
} 
+0

其實我有一個多視角,其中3次是there..this GridView控件來在第三視圖下。所以我必須添加新的功能,然後添加你的代碼或其他的東西..? – Naveen31

+0

我想你需要保存gridview中的細節數據庫按鈕點擊??對?? –

+0

其實我有一個多視圖3次,每個觀點我都要做數據庫connectivity.This GridView的部分是第三視圖,所以我怎麼能做到這一點,我必須單獨使用3種功能於所有3次或別的東西, ? – Naveen31

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事件中完成,以便您的值將存儲在數據庫中。

+0

讓我知道,如果你從這個任何想法.... – gkrishy

+0

其實我有一個多視角,其中3次是there..this GridView控件來下第三視圖。所以我必須添加新的功能,然後添加你的代碼或其他的東西..? – Naveen31

+0

你要添加什麼功能?你能否再簡要介紹一下。 – gkrishy

-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); 
+0

希望幫助你,我想知道,我已經編輯了你所說的代碼..!如果你還需要幫助.. !!我會再次準備好幫助..! 它是一個很棒的感覺..! –

+0

「命令執行過程中遇到致命錯誤」...它提供錯誤...你能解決這個問題.. – Naveen31