You must Sign In to post a response.
  • Category: ASP.NET

    Assign table column to a variable

    i want to retrieve admission number from table and assigned into a variable.
  • #767271
    Hello Krishna,

    step 1:
    public partial class Tracking_AdminSettings_WITHOUTCOMPONENT : System.Web.UI.Page
    {

    string VariableName = string.Empty;

    public void VariableAssign()
    {
    SqlConnection con = Connection.DBConn();
    string str = "select AdmissionNumber from tablename ";
    string str = "select * from tablename or where condition(name='"+YourNeeded+"')";
    DataSet ds = new DataSet();
    SqlDataAdapter sda = new SqlDataAdapter(str, con);
    sda.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
    VariableName = ds.Tables[0].Rows[0]["noofslots"].ToString();

    }
    }

    PagoLoad:
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    VariableAssign();
    }
    }
    Button Submit:
    protected void btnsubmit_Click(object sender, ImageClickEventArgs e)
    {
    VariableAssign();
    }

    if you want page load na bind the function name on Pageload else you want in Button Click na Bind the function in Button Click.

    hope this will help you

  • #767297
    Hi,

    As per my understanding you want to get the data from database and assign it into one variable.

    What you tried so far?

    Have you get data from database and store the same into dataset first?

    If not, first get the data from database and bind the same into dataset like below and then assign that value into one string variable


    protected void BindData()
    {
    cmd = new SqlCommand("your query", con);
    dt = new DataTable();
    string val=string.Empty;
    try
    {
    con.Open();
    da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
    value=dt.Rows[0][0].ToString();// I'm trying to get 0th column 0th row value and store the same into one string object.
    }

    }
    catch (Exception ex)
    {

    }
    finally
    {
    con.Close();
    con.Dispose();
    }
    }

    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments