Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to populate fields on the form on click of search button with stored procedure. At database level SP works fine. The textbox I am trying to search with is varchar type

private void btnsearch_Click(object sender, EventArgs e)
{
bool temp = false;
SqlConnection con = new SqlConnection("Data Source=xxxxxxxxxxx;Initial Catalog=xxxxxxxx;Trusted_Connection=true");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_name";


con.Open();
cmd.Connection = con;
cmd.Parameters.AddWithValue("@abc", txtabc.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//need help with this part.
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
}

I need help with populating other fields when user enters value into the textbox and click on search button if value exists in the db.

Thanks.
Posted
Comments
[no name] 5-Nov-15 13:26pm    
What issue you face here?

Access your column values with dr["ColumnOneName"].ToString() or dr[0].ToString() and populate your fields

Do you have some other requirement here?
Member 12076824 5-Nov-15 14:42pm    
I was using wrong syntax, that's working fine now. Thanks

1 solution

As mentioned in the comments, accessing the data from a DataReader is quite trivial.

For example:
C#
TextBox1.Text = dr["someField"].ToString();
TextBox2.Text = dr["someField2"].ToString();


etc.
 
Share this answer
 
Comments
Member 12076824 5-Nov-15 14:42pm    
Thanks, this is working fine
ZurdoDev 5-Nov-15 14:47pm    
Glad to hear it.

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