Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,it will be linked to search the word in google.
Now i want to send the linked values to database.
Any ideas guys?
My aspx code:
XML
<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>


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();
        }
Posted

If you are trying to get the values of the inputs from the datatable, you can do this:
C#
foreach (DataRow row in dt.Rows)
{
    Console.WriteLine(row["name"].ToString());
}
 
Share this answer
 
Comments
Dalek Dave 8-Sep-10 16:43pm    
Good Call.
pwtc222 8-Sep-10 21:04pm    
Hi.
How may i send the values in the datatable into database?
I have a code here :
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES ('" + TxtName.Text + "')";
pwtc222 8-Sep-10 21:05pm    
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES ('" + TxtName.Text + "')";
cmd.Connection = conn;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
pwtc222 9-Sep-10 2:46am    
where do i apply this code?
First of all, NEVER build a command like this:

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES ('" + TxtName.Text + "')";

Doing this exposes you to sql injection vulnerabilities. Instead, do this:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
cmd.Parameters.AddWithValue("@compName", TxtName.Text);


In regards to where to add this code, that all depends on your code. Ideally, you want to seperate your data access, business logic and presentation layers (n-Tier architecture). There are many artciles on the web that explain this architecture. Below are some articles that can help you get started:


Since you seem like you are a beginner, for testing and learning purposes, you can do this, however, I do not recommend the below method:

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
foreach (DataRow row in dt.Rows)
{
     cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
     cmd.ExecuteNonQuery();
}


After all your inserts are done, don't forget to clean up after yourself. Dispose of the objects you are done with (cmd, conn..)

cmd.Dispose();
conn.Close();
conn.Dispose();
 
Share this answer
 
Comments
pwtc222 12-Sep-10 21:56pm    
Hi.
I tried to apply your code.But it gives me error like this :

'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found (are you missing a using directive or an assembly reference?)

In the code line : cmd.Parameter.AddWithValue("@compName", row["name"].ToString());

What cause this error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900