5 pts.
0
Q:
Database connectivity ASP.NET using C#
hi i am new in asp.net,so excuse me such a stupid question. i am trying to connect a page to the database but there may be some problem please help me......

this the aspx file code

<table>
<tr>
<td style="width: 100px">
Name</td>
<td style="width: 7px">
:</td>
<td style="width: 185px">
<asp:TextBox ID="TextBox1" runat="server" Width="177px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Roll</td>
<td style="width: 7px">
:</td>
<td style="width: 185px">
<asp:TextBox ID="TextBox2" runat="server" Width="178px"></asp:TextBox></td>
</tr>
</table>
<br />
<table>
<tr>
<td style="width: 100px">
<asp:Button ID="save" runat="server" Text="Save" OnClick="btn_save" /></td>
<td style="width: 100px">
<asp:Button ID="cancel" runat="server" Text="Cancel" /></td>
</tr>
</table>


this is the aspx.cs file code

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>
/// Summary description for Connection
/// </summary>

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;
}
}
}
}
ASKED: May 18 2009  5:41 AM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
23525 pts.
0
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • AddThis Social Bookmark Button
Try this code in your Page_Load function. Create a datagrid with ID myDataGrid.

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();
}
Last Answered: May 18 2009  9:34 PM GMT by Mshen   23525 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Carlosdl   29340 pts.  |   May 18 2009  8:33PM GMT

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 ?

 

M3RL1N   30 pts.  |   May 25 2009  7:58PM GMT

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.

 
0