.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ADO.Net >> Post New QuestionBookmark and Share Subscribe to Forum

getting data table from database to datagridview

Posted By: kamlesh     Posted Date: March 31, 2011    Points:5   Category :ADO.Net
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection Conn = new SqlConnection("Data Source=sp-swd-35\\SQLEXPRESS; Database=Student; Integrated Security=SSPI");
// SqlConnection Conn;

SqlCommand sqlCmd = new SqlCommand("GetInformation", Conn);
SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Studentinfo");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "Studentinfo";
}

Responses
Author: kamlesh             
Posted Date: March 31, 2011     Points: 20   

int c;
int ID = Convert.ToInt32(textBox1.Text);
string Name = textBox2.Text;
string Address = textBox3.Text;
int PhoneNumber = Convert.ToInt32(textBox4.Text);
string EmailID = textBox5.Text;

try
{
SqlConnection Conn = new SqlConnection("Data Source=sp-swd-35\\SQLEXPRESS; Database=Student; Integrated Security=SSPI");

SqlCommand sqlCmd = new SqlCommand("SaveInformation", Conn);

sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@ID", SqlDbType.Int).Value = textBox1.Text.ToString();
sqlCmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = textBox2.Text;
sqlCmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = textBox3.Text;
sqlCmd.Parameters.Add("@PhoneNumber", SqlDbType.Int).Value = textBox4.Text.ToString();
sqlCmd.Parameters.Add("@EmailID", SqlDbType.VarChar).Value = textBox5.Text;

SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count >= 0)
{
// Studentinfo.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
MessageBox.Show("Suceed");
}
else
{
MessageBox.Show("Failed");
}
Conn.Close();
sqlCmd.Dispose();

}
catch (Exception ex)
{
MessageBox.Show("Error found In Save");
// MessageBox.Show("Exception caught in SaveMail." + ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//clsError.LogError(ex);
}


this is using for Insert Using Store Procedure

Thanks And Regards.
Kamlesh Nikam
Author: kamlesh             
Posted Date: March 31, 2011     Points: 20   

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Kamlesh Nikam
-- =============================================
ALTER PROCEDURE [dbo].[SaveInformation]
@ID int,
@Name varchar(MAX),
@Address varchar(MAX),
@PhoneNumber int,
@EmailId varchar(MAX)
AS
BEGIN
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO [dbo].[Studentinfo]
([ID],[Name],[Address],[PhoneNumber],[EmailId])
VALUES
(@ID,@Name,@Address,@PhoneNumber,@EmailId)

END


insert store procedure

Thanks And Regards.
Kamlesh Nikam
Author: kamlesh             
Posted Date: March 31, 2011     Points: 20   

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Kamlesh Nikam
-- =============================================
ALTER PROCEDURE [dbo].[GetInformation]
-- @ID int,
-- @Name varchar(MAX),
-- @Address varchar(MAX),
--@PhoneNumber int,
--@EmailId varchar(MAX)
AS
BEGIN
SET NOCOUNT ON;

SELECT * From dbo.Studentinfo
--select * from dbo.Studentinfo
END

Thanks And Regards.
Kamlesh Nikam
Author: Balwant             
Posted Date: March 31, 2011     Points: 20   

hi,
for what r u looking please elaborate.
if u want to get datatable from database and fill the grid with that table.
if your dataset has only one table then their is no need to call specific table in the dataset.
but i you have to get specific table from dataset then you can use the following code to get the table as:
DataTable dt = new DataTable;
for(int i=0;i<=ds.Tables.count;i++)
{
dt=ds.Table["Tablename"]; //you can use this table for filling the grid view.
}
dataGridView1.DataSource = dt;
dataGridView1.DataMember = "Studentinfo";

But the real problem u r facing is not mention so please mention problem if this does not help you.
Thanks
Balwant

Logic Lab technologies
Email:balwant.mnd@gmail.com
Website:www.techlogiclab.com
Author: kamlesh             
Posted Date: April 05, 2011     Points: 20   

Thanks Balwant

but i put this code for help to anyone who is facing this problem...its just a solution

Thanks And Regards.
Kamlesh Nikam
Author: SureshM             
Posted Date: June 08, 2011     Points: 20   

hi kamalesh

Just you add the Tables[0] with your code.

like this

this.dataGridView1.DataSource = ds.Tables[0];



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend