Database connectivity ASP.NET using C#

5 pts.
Tags:
ASP.NET
C
Database connectivity
Web development
Hi, I'm new in ASP.NET so excuse me if this is a stupid question. I'm trying to connect a page to the database but there may be some problem. Please help me...... This the ASPEX file cod:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using Connection; using System.IO; public partial class _hello : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_save(object sender, EventArgs e) { try { Connection.ConnectionCon con = new Connection.ConnectionCon(); String query = "insert into tbl_JobSeeker_Details(name,roll)" + " values('" + TextBox1.Text + "','" + TextBox2.Text + "' , 1)"; con.querryExecution(query); Response.Redirect("hello.aspx", false); } catch (Exception ex) { Response.Write(ex.Message); } } } this is the connection.cs page using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; /// /// Summary description for Connection /// namespace Connection { public class ConnectionCon { String constr = ConfigurationManager.ConnectionStrings["JobSearch">.ToString(); public void querryExecution(String querry) { SqlConnection con = null; SqlCommand com = null; con = new SqlConnection(constr); com = new SqlCommand(querry, con); try { con.Open(); com.ExecuteNonQuery(); con.Close(); } catch (Exception e) { string s = e.Message; con.Close(); } } public DataTable FetchDT(String query) { SqlConnection con = null; SqlDataAdapter da = null; DataTable dt = null; DataSet ds = null; try { con = new SqlConnection(constr); con.Open(); da = new SqlDataAdapter(query, con); con.Close(); ds = new DataSet(); da.Fill(ds); dt = ds.Tables[0]; return dt; } catch (Exception ex) { con.Close(); string s = ex.Message; return dt; } } public DataSet FetchDS(string query) { SqlConnection con = null; SqlDataAdapter da = null; DataSet ds = null; try { con = new SqlConnection(constr); con.Open(); da = new SqlDataAdapter(query, con); con.Close(); ds = new DataSet(); da.Fill(ds); return ds; } catch (Exception ex) { con.Close(); string s = ex.Message; return ds; } } } }

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try this code in your Page_Load function. Create a datagrid with ID myDataGrid.

<pre>string connectionString = "Data Source=(local);Initial Catalog=DBName;User ID=User;Password=Password";
SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataReader reader = null;

SqlCommand myCommand = new SqlCommand("GetData",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;

try
{
myConnection.Open();
reader = myCommand.ExecuteReader();
myDataGrid.DataSource = reader;
myDataGrid.DataBind();
}
catch(Exception ex)
{
// Catches and logs the exception
}
finally
{
reader.Close();
myConnection.Close();
}</pre>

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Why don't you tell us why you think "there may be some problem"? Are you getting errors? What are the error messages? What database are you trying to connect to?
    68,525 pointsBadges:
    report
  • M3RL1N
    Really need a little more information about whats exactly happening with your code. Copy and paste errors for the best response from everyone on here.
    30 pointsBadges:
    report
  • mohammedfayaz
    how to connect to data base in c#?
    15 pointsBadges:
    report
  • TomLiotta
    To see how to connect with C#, see http://bit.ly/Ue4xfg . -- Tom
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following