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

    How to retrievevalue from datbase and show in textboxes when click on checkbox control

    actually i have checkbox and three textboxes so i want to retrieve contactnumber from dtabase to textboxes with relative to userid if forsame id if contactnumber is 3 nos i want 3 nos to retrieve to thesethree textboxes below is my code where iam wrong plz help me

    SqlConnection con = new SqlConnection(connStr);
    SqlCommand cmd = new SqlCommand("select ContactNumber from ekthaemployeeab where ContactNumber like '%[0-9]%' and EmployeeId ='" + TextBox1.Text + "' ", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, " ekthaemployeeab");
    //TextBox10.Text = ds.Tables["ekthaemployeeab"].Rows[0]["ContactNumber"].ToString();
    //TextBox11.Text = ds.Tables["ekthaemployeeab"].Rows[1]["ContactNumber"].ToString();
    //TextBox12.Text = ds.Tables["ekthaemployeeab"].Rows[2]["ContactNumber"].ToString();
    if (ds.Tables["ekthaemployeeab"].Rows.Count > 0)
    {


    TextBox10.Text = ds.Tables["ContactNumber"].Rows[0][0].ToString();
    TextBox10.Visible = true;
    }
    if (ds.Tables["ekthaemployeeab"].Rows.Count > 1)
    {



    TextBox11.Text = ds.Tables["ContactNumber"].Rows[1][0].ToString();
    TextBox11.Visible = true;
    }
    if (ds.Tables["ekthaemployeeab"].Rows.Count > 2)
    {



    TextBox12.Text = ds.Tables["ContactNumber"].Rows[2][0].ToString();
    TextBox12.Visible = true;
    }
    if (ds.Tables["ekthaemployeeab"].Rows.Count > 2)
    {



    TextBox13.Text = ds.Tables["ContactNumber"].Rows[2][0].ToString();
    TextBox13.Visible = true;
    }
    if (ds.Tables["ekthaemployeeab"].Rows.Count > 2)
    {



    TextBox14.Text = ds.Tables["ContactNumber"].Rows[2][0].ToString();
    TextBox14.Visible = true;
    }
    con.Open();
    cmd.Executenonquery();
    con.Close();
  • #764266

    Hai VELIDIRAVIRAM,
    I dont see any issue in the code which you have posted here.
    The only problem is if you get the results, you need to use the below code to assign to the textboxes:

    TextBox10.Text = ds.Tables["ekthaemployeeab"].Rows[0]["ContactNumber"].ToString();
    TextBox11.Text = ds.Tables["ekthaemployeeab"].Rows[0]["ContactNumber"].ToString();
    TextBox12.Text = ds.Tables["ekthaemployeeab"].Rows[0]["ContactNumber"].ToString();

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764280
    what is the issue ? what is the error ? it looks your query is not so optimized, you need to change 'ContactNumber like '%[0-9]%' ' this condition
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764296

    Hi,

    there are few code errors in your code snippet, coming from last 3 lines


    con.Open();
    cmd.Executenonquery();


    you are open Connection, after run the command this is syntactically wrong, and you call the command method ExecuteNonQuery(), for select statement this is too wrong.

    I suggest you to open the connection before run the command and store those records into dataset and based on rows you can assign it to controls.


    SqlConnection con = new SqlConnection("connection string");
    SqlCommand cmd =new SqlCommand("Select statement");
    con.Open();
    SqlDataAdapter da= new SqlDataAdapter(cmd, con);
    DataSet ds=new DataSet();
    da.Fill(ds);
    if(ds.Tables.Count >0 && ds.Tables[0].Rows.Count>0)
    {
    txt1.Text=ds.Tables[0].Rows[0]["Col1"].ToString();
    }
    con.Close();

    Try something like above to achieve your goal...


    --------------------------------------------------------------------------------
    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