anil wagavkar

anil wagavkar

  • NA
  • 13
  • 5.2k

how to insert db

Mar 10 2016 6:33 AM
in dropdownlist contain product name.i have to save the product code in my db.
and also when i am select product name from dropdownlist prize of that product come automatically.
in prize textbox. and prize save in db also
 
 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class visa_master_customer_master : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=F:\\anilproject\\inventory\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getbind();
}

}
private void getbind()
{
SqlCommand cmd = new SqlCommand("select * from productmaster", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
DropDownList1.DataValueField = "price";
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
//int price =Convert.ToInt32(DropDownList1.SelectedValue);
string price = Convert.ToString(DropDownList1.SelectedValue);

SqlCommand cmd = new SqlCommand("insert into acmaster(name,addr1,mobile,panono,nominee,nomineer,custno,prize,pname)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + price+ "','"+id+"')", con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
Label3.Text = "Data Inserted..";
con.Close();

}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox8.Text = DropDownList1.SelectedValue.ToString();
}

Attachment: new folder.zip

Answers (1)