Mohamed Mitwalli
Привет ,
Проверить это
<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();
}
}
}
с уважением
М. Митвалли
Nandhu_nands
привет, мне нужно объединить значение текстового поля antoher в параметр @id
как это сделать
cmd.параметры.AddWithValue("@id", DropDownList1.SelectedValue"здесь);