Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir i am using window form and c#, i want to insert id into the textbox and rest of data from the database should be automatic display.
sir how to do it without using datagridview control.
please give me some examples

What I have tried:

public void selectdata()
       {

           string connection = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
           using (SqlConnection con = new SqlConnection(connection))
           {
               con.Open();
               using (SqlCommand cmd = new SqlCommand("select ITEM_NAME from ITEMOLD$ where ITEM_CODE='" + txtitemcode.Text + "'", con))
               {
                   SqlDataReader dr = cmd.ExecuteReader();
                   while (dr.Read())
                   {
                       txtitemname.Text = Convert.ToString(dr["ITEM_NAME"]);
                       txtpartycode.Text = Convert.ToString(dr["PARTY_CODE"]);
                       txthsnno.Text = Convert.ToString(dr["HSNNO"]);


                       txtsalesprice.Text = Convert.ToString(dr["SALE_PRICE"]);
                       txttotal.Text = Convert.ToString(dr["DISC_AMT"]);
                       txtcgst.Text = Convert.ToString(dr["CGST"]);
                       txtigstdata.Text = Convert.ToString(dr["IGST"]);


                   }

               }
           }
       }
Posted
Updated 29-Jun-17 1:38am

1 solution

Column names missing in the select query
select ITEM_NAME ,PARTY_CODE ,HSNNO,SALE_PRICE,DISC_AMT,CGST,IGST from ITEMOLD$ 

Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

using (SqlCommand cmd = new SqlCommand("select ITEM_NAME ,PARTY_CODE ,HSNNO,SALE_PRICE,DISC_AMT,CGST,IGST from ITEMOLD$  where ITEM_CODE=@code ", con))
           {
               cmd.Parameters.AddWithValue("@code", txtitemcode.Text.Trim());
 
Share this answer
 

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