Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to get the data from the database.

For that i build one stored procedure as

SQL
ALTER Procedure [dbo].[Select_Students](@Sno int = null)
As
Begin
If @Sno Is Null
Select Sno,Sname,Class,Fees From Stud 
Else
Select Sname,Class,Fees From Stud Where Sno=@Sno
End


And the code under get button as

C#
private void button1_Click(object sender, EventArgs e)
        {
            cmd.Parameters.Clear();
            ds = new DataSet();
            da = new SqlDataAdapter();
            cmd.CommandText = "Select_Students";
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Stu");
            string value_of_sno = textBox1.Text;
            cmd.Parameters.Add("@sno", value_of_sno);
            
              if (ds.Tables[0].Rows.Count > 0)
            {
                textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
                textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
                textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
            }
            else
                MessageBox.Show("Record Not Existed");
        }

But always i'm getting the first record values only.
I think textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
due to this code only i'm getting the first record values.
can one help me to get the record values depending on the value entered by me in the first textbox.
Posted
Updated 17-Aug-11 0:56am
v2
Comments
Herman<T>.Instance 17-Aug-11 6:58am    
when @sno is null you will get a full list. Which value do you wnat to show then?
and your cmd.Parameters.Add() line should be set before you start with da = new SqlDataAdapter(cmd);

You have messed up your code. Adding parameter after Fill serves nothing.
Try this
C#
cmd.Parameters.Clear();
cmd.CommandText = "Select_Students";
string value_of_sno = textBox1.Text;
cmd.Parameters.Add("@sno", value_of_sno);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "Stu");
 
Share this answer
 
v2
change
C#
if (ds.Tables[0].Rows.Count > 0)
            {
                textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
                textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
                textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
            }


to:
C#
if (ds.Tables[0].Rows.Count > 0)
{
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        if (row["sno"].ToString() == textbox1.Text)
        {
            textBox2.Text = row[1].ToString();
            textBox3.Text = row[2].ToString();
            textBox4.Text = row[3].ToString();
            break; // get out of the foreach
        }
    }
}
 
Share this answer
 
Comments
usha nelavalli 17-Aug-11 7:16am    
i changed the code
if (row["sno"].ToString() == textbox1.Text)
in this line it shows the error that sno is not the parameter for stud.
please help me to overcome this error
Herman<T>.Instance 17-Aug-11 7:26am    
if you debug, what is the value for row["sno"] and for row[0] ?
if the value of textBox1.Text is blank, then do not set the parameter.
C#
string value_of_sno = textBox1.Text;
            cmd.Parameters.Add("@sno", value_of_sno);

and suggested by digimanu, set the parameters at the proper places.
 
Share this answer
 
@usha nelavalli : Plz check that your data set is fill with all the records(by clicking on it in debugging mode)if it is fill with all the records the it will show result as u want.
 
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