如何从下拉列表中向数据库添加值? [英] how to add value to database from dropdown list ?

查看:80
本文介绍了如何从下拉列表中向数据库添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个下拉列表,其中我在游戏中添加了两个值

1. XL,2。 XXl

通过选择任何值并单击OK按钮,必须在数据库表大小中添加该值。



应该是什么编码......... ???



waitng for ur reply .... guys。

解决方案



查看此

 <   div  >  
< asp:DropDownList ID = DropDownList1 runat = server >
< / asp:DropDownList >
< asp:按钮 ID = Button1 runat = server < span class =code-attribute>文本 = 按钮 onclick = Button1_Click / >
< / div >



 受保护 < span class =code-keyword> void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetData();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = name;
DropDownList1.DataValueField = id;
DropDownList1.DataBind();
}

}
DataTable GetData()
{
DataTable dt = new DataTable ();
使用(SqlConnection con = new SqlConnection( @ 数据源=。;初始目录=测试;持久安全信息=真;用户ID = sa;密码= ***))
{
con.Open();
使用(SqlCommand cmd = new SqlCommand( select * from Product,con))
{
SqlDataAdapter adpt = SqlDataAdapter(cmd);
adpt.Fill(dt);
}

}
return dt;
}
受保护 void Button1_Click( object sender,EventArgs e)
{
using (SqlConnection con = new SqlConnection( @ Data Source = .\SQLEXPRESS; Initial Catalog = test; Persist Security Info = True ;用户ID = sa;密码= ***))
{
con.Open();
使用(SqlCommand cmd = new SqlCommand( 插入Testtbl(id)值(@id),con))
{
cmd.Parameters.AddWithValue( @ id,DropDownList1.SelectedValue);
cmd.ExecuteNonQuery();

}

}
}



最好的问候

M. Mitwalli


http:// stackoverflow.com/questions/10586038/asp-net-adding-database-from-textbox-and-dropdownlist [ ^ ]

http://www.dotnetfunda.com/forums/thread4161-how-to-bind-database-table-to -dropdownlist-value.aspx [ ^ ]


试试这个:



按钮点击:



 String s =insert into tablename values('+ Drop1.SelectedItem.Value +'); 
Sqlcommand cmd = new Sqlcommand(s,Connection Object);
cmd.ExecuteNonQuery();





希望这会帮助你解决你的Prblm ...


hey, i have one dropdownlist in which i hav added two value in the fild
1. XL ,2. XXl
as by selecting anyone value and clicking on OK button the value must be added in the database table size.

what should be the coding .........???

waitng for ur reply....guys.

解决方案

Hi ,
Check this

<div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          DataTable dt =  GetData();
          DropDownList1.DataSource = dt;
          DropDownList1.DataTextField = "name";
          DropDownList1.DataValueField = "id";
          DropDownList1.DataBind();
        }

    }
    DataTable GetData()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=***"))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("select * from Product ",con))
            {
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                adpt.Fill(dt);                
            }
            
        }
        return dt;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=***"))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("insert into Testtbl (id) values (@id) ", con))
            {
                cmd.Parameters.AddWithValue("@id", DropDownList1.SelectedValue);
                cmd.ExecuteNonQuery();

            }

        }
    }


Best Regards
M.Mitwalli


http://stackoverflow.com/questions/10586038/asp-net-adding-database-from-textbox-and-dropdownlist[^]
http://www.dotnetfunda.com/forums/thread4161-how-to-bind-database-table-to-dropdownlist-value.aspx[^]


Try This :

On Button Click :

String s="insert into tablename values('"+Drop1.SelectedItem.Value+"')";
Sqlcommand cmd=new Sqlcommand(s,Connection Object);
cmd.ExecuteNonQuery();



Hope this will help you and solve your Prblm...


这篇关于如何从下拉列表中向数据库添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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