Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm Trying to get data from a Database.

I wrote the stored Procedure for Selecting the values from the table as
ALTER Procedure [dbo].[Select_Students](@Sno int)
As
Begin
If @Sno Is Null
Select Sno,Sname,Class,Fees From Stu 
Else
Select Sname,Class,Fees From Stu Where Sno=@Sno
End

and code for the Get button as
private void button1_Click(object sender, EventArgs e)
        {
            cmd.Parameters.Clear();
            ds = new DataSet();
            da = new SqlDataAdapter();
            cmd.CommandText = "Select_Students";
            da.Fill(ds, "Stu");
            
            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 the data is not coming from the database.

Can any one tell me the problem in this code.

Thank you so much
Posted
Updated 16-Aug-11 21:33pm
v3
Comments
nagendrathecoder 17-Aug-11 3:08am    
Have to tried to execute procedure in database?
Try to execute it and check whether its showing data or not.
vivek_cool 17-Aug-11 3:09am    
use google
nagendrathecoder 17-Aug-11 3:13am    
You have replied to me. :D
vivek_cool 17-Aug-11 3:14am    
yes
nagendrathecoder 17-Aug-11 3:15am    
i guess you wanted to tell this to OP right.

 
Share this answer
 
I added your code

C#
ds = new DataSet();
da = new DataAdapter(cmd);
da.Fill(ds, "Stu");


and I executed my stored procedure in database there it get executed nicely.
after adding your code it displays the top record values for all.
 
Share this answer
 
v2
Comments
nagendrathecoder 17-Aug-11 3:30am    
it will display the records of the "sno" you have passed to stored procedure.
So i guess you are getting correct result.
Do you want anything else too?
nagendrathecoder 17-Aug-11 3:32am    
You can comment on my answer, you don't need to add new answer for that.
You want to assign the command object to sqlDataAdapter

C#
da = new SqlDataAdapter(cmd);


Hope this helps!
 
Share this answer
 
Comments
nagendrathecoder 17-Aug-11 3:14am    
She is clearing parameters and later not passing any parameter.
Code is not correct.
Where are you passing the parameter to stored procedure.
Try using this code:

//After creating SqlCommand object and adding parameters to it
ds = new DataSet();
da = new DataAdapter(cmd);
da.Fill(ds, "Stu");
 
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