4
Answers

how to retrive value from db when value select from dropdown

anil wagavkar

anil wagavkar

9y
492
1
in my project when salesmans select from dropdownlist salemans id and its position come , select product name from dropdownlist product id and prize come sucefully. i have to show commission of that salesmans in textbox form commiion_chart table.
 
 
plz help.   my code is below.
 
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=103.209.144.111;Persist Security Info=true;User ID=sparsh;Password=Visa@1234");

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getbind();
productbind();
salsmanbind();



}

}

private void productbind()
{
SqlCommand cmd= new SqlCommand("select * from itemmaster",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource=dt;
DropDownList1.DataValueField="id";
DropDownList1.DataTextField="name";
//DropDownList1.DataValueField = "price";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("------Select Product---------", "0"));


}
private void salsmanbind()
{
SqlCommand cmd = new SqlCommand("select * from salesmans_master", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "rankcode";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("------Select Salemans---------", "0"));

}
private void getbind()
{
SqlCommand cmd = new SqlCommand("Select id,Name,Addr1,Mobile from acmaster", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into acmaster(Name,Addr1,Mobile,Emailid,Panno,Nominee,Relation,Sponcerid,Sponcustno,Itemcode,Itemprice)values('" + customer.Text + "','" + adderss.Text + "','" + mobile.Text + "','" + emailid.Text + "','" + panno.Text + "','" + nomineen.Text + "','" + nomineer.Text + "','" + DropDownList2.SelectedItem.Value + "','" + sponcustno.Text + "','" +DropDownList1.Text+ "','" + prodid.Text + "')", con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
Label10.Text = "Data Inserted..";
con.Close();
clearfield();
}
catch (Exception ex)
{
Label10.Text = ex.Message;
}
}
private void clearfield()
{
prodid.Text = DropDownList1.Text = sponcustno.Text = nomineen.Text = nomineer.Text = panno.Text = emailid.Text =

mobile.Text = adderss.Text = customer.Text = "";
}

protected void Button2_Click(object sender, EventArgs e)
{

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
SqlCommand cmd = new SqlCommand("delete from acmaster where id=" + id, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
getbind();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


prodid.Text = DropDownList1.SelectedValue.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("select price from itemmaster where id=" + prodid.Text);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
prodprize.Text = dr["price"].ToString();
con.Close();
}




}
protected void nomineen_TextChanged(object sender, EventArgs e)
{

}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{

rankid.Text = DropDownList2.SelectedValue.ToString();

con.Open();

SqlCommand cmd = new SqlCommand("select rank_name from rank_master where id=" + rankid.Text);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
rankname.Text = dr["rank_name"].ToString();
con.Close();
}

}
// private void getcommission()
//{
// con.Open();
// SqlCommand cmd = new SqlCommand ("Select Commission from commission_chart where Rankid='" +rankid.Text + "' and Productid='" + prodid.Text + "'", con);
// cmd.Connection=con;
// SqlDataReader dr = cmd.ExecuteReader();
// if(dr.Read())
// {
// commission.Text=dr["Commission"].ToString();
// con.Close();
// }
//}






protected void commission_TextChanged(object sender, EventArgs e)
{

}
}

 
 
 
 
 
Answers (4)
0
ali tuncer

ali tuncer

NA 2.9k 108 9y

I don't think you are calling it because it is commented out...I don't know what your form looks like, I guess you can call it in Button2_Click..there is nothing in it..it is better to make sure rankid and prodid is not empty otherwise query will be wrong..

protected void Button2_Click(object sender, EventArgs e)
   if(!string.IsNullOrEmpty(rankid.Text) && !string.IsNullOrEmpty(prodid.Text))
   {
           getcommission();
   }
 
0
anil wagavkar

anil wagavkar

NA 13 2.9k 9y
This is my getcommision method.
 
 
private void getcommission()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Commission from commission_chart where Rankid='" + rankid.Text + "' and Productid='" + prodid.Text + "'", con);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
commission.Text = dr["Commission"].ToString();
con.Close();
}
}
 
but i still confued where it is called....... 
 
 
 
 
 
 
 
 
0
ali tuncer

ali tuncer

NA 2.9k 108 9y
you wrote a method to show commission(getcommission), where do you call it? is it working? check the query, make sure you get product and rankid..
0
Sagar Pardeshi

Sagar Pardeshi

NA 16.6k 3.4m 9y
Hello,
 

bind / fill / populate data to DropDownList control from database records .

 
ref following url:
 
http://www.aspsnippets.com/Articles/Bind-Fill-Populate-DropDownList-control-from-database-in-ASPNet-using-C-and-VBNet.aspx