Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example i am having a drop down box ,
eg: i am selecting product name in drop down, ie:ice cream
The price of the ice cream have to display in text box , that data have to retrieve from database.

This is the code , which i used , but data not retrieve :
I need the solution , i need your help.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;


public partial class Default2 : System.Web.UI.Page
{

    public string connectionString = WebConfigurationManager.ConnectionStrings["conWeb"].ConnectionString;
    public SqlConnection cn;
    public SqlCommand cmd;

    #region DECLARATIONS
    private string mailTo;
    private string mailFrom;
    private string mailMessage;
    private string mailSubject;
    private string mailReply;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                cn = new SqlConnection(connectionString);
                string strQuery = "Select id,Tabletname,Price from tb_addproduct";
                SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
                DataSet ds = new DataSet();
                cn.Open();
                da.Fill(ds);
                ddllist.DataSource = ds;
                ddllist.DataTextField = "Tabletname";
                txtprice.ReadOnly.ToString();
                ddllist.DataValueField = "id";
                ddllist.DataBind();
                ddllist.Items.Insert(0, "--Select Tablet--");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    protected void btsubmit_Click(object sender, EventArgs e)
    {
       
        if (SubmitDetails())
        {
            lblMsg.Visible = true;
            lblMsg.ForeColor = System.Drawing.Color.Blue;
            lblMsg.Font.Bold = true;
            lblMsg.Font.Size = 10;
            lblMsg.Text = "Your request has been submitted";
            //ClearValues(this);
            ddllist.Text = "";
            Qty1.Text = "";
            txtprice.Text = "";
            email.Text = "";
            ads.Text = "";
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.ForeColor = System.Drawing.Color.Red;
            lblMsg.Font.Bold = true;
            lblMsg.Font.Size = 14;
            lblMsg.Text = "Try Again Sometime Later";
        }
    }
    private bool SubmitDetails()
    {
        try
        {
            mailTo = "vinoth.lakshman86@gmail.com";
            mailFrom = email.Text;
            mailSubject = "Order Details";
            mailReply = email.Text;
            mailMessage = "<table><tr><td height='4'>&nbsp;</td></tr><tr><td></td></tr><tr><td height='8'></td></tr><tr><td align='center'><table><tr><td>Tablet 1:</td><td>"
                + txtprice.Text + "</td></tr><tr><td>Price:</td><td>"
                + ddllist.Text + "</td></tr><tr><td>Quantity:</td><td>"
                + Qty1.Text + "</td></tsr><tr><td>Emailid:</td><td>"
                + email.Text + "</td></tsr><tr><td>Address:</td><td>"
                + ads.Text + "</td></tr></table></td></tr></table>";
            MailMessage myMailMessage = new MailMessage(mailFrom, mailTo);
            myMailMessage.Subject = mailSubject;
            myMailMessage.Body = mailMessage;
            myMailMessage.IsBodyHtml = true;
            myMailMessage.ReplyTo = new MailAddress(mailReply);
            // They recommend all mails from this mail ID...
            System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential("vinoth.lakshman86@gmail.com", "test1234");
            //They provide this host name and port no....
            System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
            mailClient.EnableSsl = true;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = mailAuthentication;
            mailClient.Send(myMailMessage);
            return true;
        }
        catch
        {
            return false;

        }
    }
    protected void ddllist_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        try
        {
            cn = new SqlConnection(connectionString);
            string strQuery = "Select id,Price from tb_addproduct where Price='" + txtprice.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
            DataSet ds = new DataSet();
            cn.Open();
            da.Fill(ds);
            da.SelectCommand.Parameters.AddWithValue("id", Convert.ToInt32(ddllist.SelectedValue.ToString()));
            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    void FullGrid()
    {
        try
        {
            cn = new SqlConnection(connectionString);
            string strQuery = "Select id,Tabletname,Price from tb_addproduct where pid='" + ddllist.SelectedItem.Value + "' and Price ='"  + txtprice.Text + "'" ;
            SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
            DataSet ds = new DataSet();
            cn.Open();
            da.Fill(ds);
          
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void datashow()
    {


        string nameshow = ddllist.Text.ToString().Trim();

        cmd.CommandText = "select * from tb_addproduct where Tabletname='" + nameshow + "' ";

        cmd.Connection = cn;
        SqlDataAdapter da = new SqlDataAdapter();

        DataSet ds = new DataSet();
        da = new SqlDataAdapter(cmd);

        da.Fill(ds);

        cn.Open();

        cmd.ExecuteNonQuery();
        ddllist.DataSource = ds;
        ddllist.DataBind();
        cn.Close();
    }
}
Posted
Updated 25-Jul-11 18:45pm
v2
Comments
[no name] 26-Jul-11 0:58am    
Use Data GridView to display data which is present in dataset.
Uday P.Singh 26-Jul-11 1:28am    
are you getting any error message? if yes then post it

protected void ddllist_SelectedIndexChanged(object sender, EventArgs e)
{
  try
  {
    cn = new SqlConnection(connectionString);
    string strQuery = "Select id,Price from tb_addproduct where Price='" + txtprice.Text + "'";
    DatatTable dt=new ClsForConnection().GenerateDataTable(strQuery );
    
    if(dt.Rows.Count>0)
    {
      txtprice.Text=dt.Rows[0]["Price"].ToString();
    }
            
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

//in clsForConnection Class
 public DataTable GenerateDataTable(string SqlStr)
    {
        try
        {
            SqlDataAdapter m_SdaDept = new SqlDataAdapter(SqlStr, cn);
            Mcon.Open();
            DataTable m_DtDept = new DataTable();
            m_SdaDept.Fill(m_DtDept);
            return m_DtDept;
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            Mcon.Close();
        }
    }
 
Share this answer
 
v3
Comments
vinoth04 26-Jul-11 2:03am    
I am getting error in this line
DatatTable dt = new ClsForConnection().GenerateDataTable(strQuery);
vinoth04 26-Jul-11 2:11am    
I am getting error in this line DatatTable dt = new ClsForConnection().GenerateDataTable(strQuery);
Aswathi Narayan 26-Jul-11 3:11am    
ClsFor Connection is a class where i write codings for connection.. You need not create that class. Better u add the function GenerateDataTable in your code page itself and instead of DatatTable dt=new ClsForConnection().GenerateDataTable(strQuery ); write

DatatTable dt=GenerateDataTable(strQuery );
ClsFor Connection is a class where i write codings for connection.. You need not create that class. Better u add the function GenerateDataTable in your code page itself and instead of DatatTable dt=new ClsForConnection().GenerateDataTable(strQuery ); write
DatatTable dt=GenerateDataTable(strQuery );

<pre>public DataTable GenerateDataTable(string SqlStr)
{
try
{
SqlDataAdapter m_SdaDept = new SqlDataAdapter(SqlStr, cn);
cn.Open();
DataTable m_DtDept = new DataTable();
m_SdaDept.Fill(m_DtDept);
return m_DtDept;
}
catch (Exception)
{
return null;
}
finally
{
cn.Close();
}
}</pre>
 
Share this answer
 
v4
Comments
vinoth04 26-Jul-11 4:09am    
How to do ?.
Let m know. i am new to dot net.
Aswathi Narayan 26-Jul-11 4:23am    
protected void ddllist_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cn = new SqlConnection(connectionString);
string strQuery = "Select id,Price from tb_addproduct where Price='" + txtprice.Text + "'";
DatatTable dt=GenerateDataTable(strQuery );

if(dt.Rows.Count>0)
{
txtprice.Text=dt.Rows[0]["Price"].ToString();
}

}
catch (Exception ex)
{
throw ex;
}
}

//in clsForConnection Class
public DataTable GenerateDataTable(string SqlStr)
{
try
{
SqlDataAdapter m_SdaDept = new SqlDataAdapter(SqlStr, cn);
cn.Open();
DataTable m_DtDept = new DataTable();
m_SdaDept.Fill(m_DtDept);
return m_DtDept;
}
catch (Exception)
{
return null;
}
finally
{
cn.Close();
}
}

just copt this code to your .cs page. Note that U have to set your dropdownlist property Autopostback as true
vinoth04 26-Jul-11 12:39pm    
Thank u Aswathi Narayan.
Now i am getting the price amount in my textbox.
But i am getting the first price amount for all thing , wt to do?.
we need to change anything in
txtprice.Text=dt.Rows[0]["Price"].ToString();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900