sam

sam

  • NA
  • 202
  • 0

Save temporary values in gridview to database

Sep 13 2010 9:38 PM
Hi.
I have a code here,where it will store temporary data in a gridview from textbox where when the data is stored temporarily in gridview.
Alternately,it will be linked to search the word in google.
Now i want to save the values in the gridview to database.
Any ideas guys?

Here is my .aspx code:
<body>
<div>
<table >
<tr>
<td class="style1">
<asp:Label ID="Label1" runat="server" Text="Caller Info :"></asp:Label>
</td>
<td style="width: 100px">
<asp:TextBox ID="TxtName" runat="server" class="cls" onekeypress="" ToolTip="Press Enter key for new input"></asp:TextBox>
</td>
</tr>
</table>

<asp:Button ID="BtnAddToTable" runat="server" align="right" class="cls"
Text="Add" OnClick="BtnAddToTable_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Caller Information">
<ItemTemplate>
<a onclick='document.getElementById("<%=iframe1.ClientID%>").src = this.href; return false;' href='http://www.google.com/#q=<%#DataBinder.Eval(Container.DataItem,"name")%>'>
<%#DataBinder.Eval(Container.DataItem,"name")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server" Text="Clear" OnClick="BtnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<iframe runat="server" id="iframe1" src="" class="cls"
style="z-index: 105; left: 225px; position: absolute; top: 109px; width: 875px; height: 455px;" frameborder="1" title="Google Search Display" ></iframe>
</div>
<asp:Button ID="BtnSendToDatabase" runat="server" class="cls" Text="//Sent to database" OnClick="BtnSendToDatabase_Click" />
</body>

And this is my .cs code:
static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
{
if (!Page.IsPostBack)
{
//Instantiating the DataTable;
dt = new DataTable("Table1");

//Adding Columns
dt.Columns.Add("name", typeof(string));

}
}
}
protected void BtnAddToTable_Click(object sender, EventArgs e)
{
dt.Rows.Add(TxtName.Text);
BindData();
TxtName.Text = String.Empty;

}

public void BindData()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void BtnDelete_Click(object sender, EventArgs e)
{
string sid = string.Empty;
string sname = string.Empty;
Button cb = sender as Button;
GridViewRow grow = (GridViewRow)cb.NamingContainer;
dt.Rows.RemoveAt(grow.RowIndex);
BindData();
}

Answers (32)