Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi


I create a database and procedure now i want to retrieve all value using asp.net using data adaptor How i do that

Please help me




Thanks


Gurpreet Arora
Posted
Comments
syed shanu 3-Nov-14 3:21am    
Use Google,Lots of sample available there.if you really cont find any solution of your question using google then replay here will give you sample.
Arora1992 3-Nov-14 3:25am    
I write this code but not working


nt Id = Convert.ToInt32(TextBox1.Text);


DataTable dt = new DataTable();

SqlConnection abc = new SqlConnection(@"connection string");
abc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = abc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "contact_retrieve_all_procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
int a = dt.Rows.Count;

for (int i = 0; i < a; i++)
{
Response.Write(dt.Rows[i]["Id"] + " " + dt.Rows[i]["title"] + "</br>" + dt.Rows[i]["Firstname"] + " " + dt.Rows[i]["Lastname"] + "</br>" + dt.Rows[i]["address"] + "</br>" + dt.Rows[i]["company"] + "</br>" + dt.Rows[i]["lastsenddate"] + "</br>");
}
syed shanu 3-Nov-14 3:26am    
what error did you get
Arora1992 3-Nov-14 3:27am    
Procedure contact_retrieve_all_procedure has no parameters and arguments were supplied.
syed shanu 3-Nov-14 3:27am    
can you paste your procedure here.

Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable", con))
        {
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
        }
    }
 
Share this answer
 
Using data adapter you can retrieve data like

SqlConnection con = new SqlConnection();
con.ConnectionString="your_connection_string";
String str="your select query";
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
 
Share this answer
 
Comments
Arora1992 3-Nov-14 4:12am    
int Id = Convert.ToInt32(TextBox1.Text);


DataTable dt = new DataTable();

SqlConnection abc = new SqlConnection(@"connection string");
abc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = abc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "contact_retrieve_procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
int a = dt.Rows.Count;

for (int i = 0; i < a; i++)
{
Response.Write(dt.Rows[i]["Id"] + " " + dt.Rows[i]["title"] + "</br>" + dt.Rows[i]["Firstname"] + " " + dt.Rows[i]["Lastname"] + "</br>" + dt.Rows[i]["address"] + "</br>" + dt.Rows[i]["company"] + "</br>" + dt.Rows[i]["lastsenddate"] + "</br>");
}



and Procedure is

create procedure contact_retrieve_procedure(@id int)as
select * from contact where Id=@Id
Praveen_P 3-Nov-14 4:20am    
can u change your procedure like this

ALTER PROCEDURE contact_retrieve_all_procedure
@Id int = 0

AS
BEGIN
select * from contact where id=@Id

END
Arora1992 3-Nov-14 4:25am    
I dont want to change the procedure I only want to retrieve data
Praveen_P 3-Nov-14 4:37am    
i didn't see any error in your code , thats why i ask to update your procedure
Arora1992 3-Nov-14 4:55am    
Its working Now Thank You

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