将临时链接的项目数据发送到数据库 [英] Send temporary linked item data to database

查看:86
本文介绍了将临时链接的项目数据发送到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我在这里有一个代码,它将在文本框中将临时数据存储在gridview中,当数据临时存储在gridview中时,它将被链接以在google中搜索单词.
现在我想将链接的值发送到数据库.
有想法吗?
我的aspx代码:

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:

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



我的CS代码:



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

推荐答案

如果您试图从数据表中获取输入值,则可以执行以下操作:
If you are trying to get the values of the inputs from the datatable, you can do this:
foreach (DataRow row in dt.Rows)
{
    Console.WriteLine(row["name"].ToString());
}


首先,切勿构建像这样的命令:

First of all, NEVER build a command like this:

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


这样做会使您暴露于sql注入漏洞.相反,请执行以下操作:


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



关于在何处添加此代码,这全都取决于您的代码.理想情况下,您要分离数据访问,业务逻辑和表示层(n层体系结构).网络上有许多文章可以解释这种架构.以下是一些可以帮助您入门的文章:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆