I created a Firebird database and successfully connected to it via Visual Studio Server Explorer. Now I want to test it through code, so I made a simple form that - on a button press - changes a label text to a value from the database. Here is the code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=localhost;initial catalog=D:\\poslovanje\\POSLOVANJE.FDB;user id=SYSDBA");
SqlCommand cmd = new SqlCommand("SELECT ID FROM USERS", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
label3.Text = dr[0].ToString();
}
con.Close();
}
Problem is my application just freezes when it comes to con.Open();
I have also tried this connection string:
User=SYSDBA;Password=masterkey;Database=D:\\poslovanje\\poslovanje.fdb;Data Source=localhost;
It's basic beginners mistake, i need to use FbConnection and FbCommand, not Sql
Related
I am creating a web application in Microsoft Visual Studio 2010. I want to retrieve data from a SQL Server database and display it in a GridView in ASP.NET using C#.
I want to retrieve data on button onclick event.
protected void submit_click(object sender, EventArgs e)
{
connection1.open();
SqlCommand sq= new SqlCommand("select student_id
, student_name from student);
SqlDataReader dr= new SqlDataReader();
...
// Gridview
}
Refer for description : Microsoft Visual Studio-how-to-retrieve-and-display-values-from-database-in -ASP.NET-using-C#.
protected void Button1_Click( object sender, EventArgs e) {
//connection string
SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS;
AttachDbFilename='C:\Users\HP\Documents\Visual Studio
2010\Projects
\assignment\assignment\App_Data\pokedex.mdf';Integrated
Security=True; User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from pokedex;");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
I am trying to create a webpage that allow users to unsubscribe from receiving the report.
So far I have created the page. users are able to see all the reports that they are receiving
I have also written an "update replace" statement that, unfortunately this updates everything.
My question is how do I modify my code to only delete one line at a time even if the same info is in the next line
Please see my code below
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
System.Web.UI.WebControls.Label rollno = GridView1.Rows[e.RowIndex].FindControl("Label1") as System.Web.UI.WebControls.Label;
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Subscriptions SET Subscriptions.ExtensionSettings = REPLACE('Subscriptions.ExtensionSettings', #ReportSearch, '') WHERE Subscriptions.ExtensionSettings like '#ReportSearch'" ;
cmd.Parameters.AddWithValue("ReportSearch", TxtSearch.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
this is the part of the code that i am sharing for the solution of my new application.
when i execute this code,it throws exception "Must declare the scalar variable #textBox1"
public partial class About : Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=.;Initial Catalog=carpro;Integrated Security=True";
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into Driver_Registration(Driver_Name,Driver_DOB,Driver_Address,License_Number,National_Insurance_Number,"+
"Email_Address,UK_History,Contact,Occupation,License_Details,Taxi_License_Number,"+
"Deposit_Details,Weekly_Rent) Values (#TextBox1,#TextBox2,#TextBox3,#TextBox4,#TextBox5," +
"#TextBox6,#TextBox7,#TextBox8,#TextBox9,#TextBox10,#TextBox11,#TextBox12,#TextBox13)",con);
cmd.Parameters.AddWithValue("#Driver_Name",TextBox1.Text);
cmd.Parameters.AddWithValue("#Driver_DOB", TextBox2.Text);
cmd.Parameters.AddWithValue("#Driver_Address", TextBox3.Text);
cmd.Parameters.AddWithValue("#License_Number", TextBox4.Text);
cmd.Parameters.AddWithValue("#National_Insurance_Number", TextBox5.Text);
cmd.Parameters.AddWithValue("#Email_Address", TextBox6.Text);
cmd.Parameters.AddWithValue("#UK_History", TextBox7.Text);
cmd.Parameters.AddWithValue("#Contact", TextBox8.Text);
cmd.Parameters.AddWithValue("#Occupation", TextBox9.Text);
cmd.Parameters.AddWithValue("#License_Details", TextBox10.Text);
cmd.Parameters.AddWithValue("#Taxi_License_Number", TextBox11.Text);
cmd.Parameters.AddWithValue("#Deposit_Details", TextBox12.Text);
cmd.Parameters.AddWithValue("#Weekly_Rent", TextBox13.Text);
cmd.ExecuteNonQuery();
Label1.Text = "Registration Successfull";
con.Close();
}
}
The parameters you are using in your INSERT statement are not the same parameters you are creating in your cmd.Parameters.AddWithValue(..). Replace the INSERT parameters to the ones you actually created like #Driver_Name.
Also, you should move your
con.ConnectionString = "Data Source=.;Initial Catalog=carpro;Integrated Security=True";
con.Open();
down to your button. Essentially you are only opening your connection once, then closing it on button click. Currently, there is no way to re-open the connection without re-starting the application.
Or better yet segment it off into its own class/function.
Or even better, setup the connection string in the web.config and call it from there utilizing dependency injection.
in your query you specify the parameters:
(#TextBox1,#TextBox2,#TextBox3,#TextBox4,#TextBox5," +
"#TextBox6,#TextBox7,#TextBox8,#TextBox9,#TextBox10,#TextBox11,#TextBox12,#TextBox13)
you have 2 options
1 you can change your query to :
VALUES(#Driver_Name,......
2 you can change you AddWithValue to:
cmd.Parameters.AddWithValue("#TextBox1",TextBox1.Text);
...
I am trying basic database insert and this code is waht I am running in visual studio 2010:-
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\WebSites\\WebSite3\\App_Data\\name.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = new SqlCommand("insert into names values('" + TextBox1.Text + "')");
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
where am I wrong?
You created a connection and opened it, but you did not associate it with the SqlCommand. You can do this a couple of ways, either in the constructor of the SqlCommand or through the Connection property of the SqlCommand.
Additionally, you should use parameterized queries to prevent SQL Injection attacks. I'd also recommend putting the SqlConnection in a using block to ensure it is closed and properly disposed of. Putting all of that together gives you something like this:
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\WebSites\\WebSite3\\App_Data\\name.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into names values(#name)", conn);
// Alternatively, you could do cmd.Connection = conn if you didn't pass
// the connection object into the SqlCommand constructor
cmd.Parameters.AddWithValue("#name", TextBox1.Text);
cmd.ExecuteNonQuery();
}
}
You did not assign the connection to the command object. try:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\WebSites\\WebSite3\\App_Data\\name.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = new SqlCommand("insert into names values('" + TextBox1.Text + "')");
cmd.Connection = conn; // <- this is the missing line
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
I am trying to connect a C# application to my MySQL database located in a remote server. When I try to execute this simple program, I get the following error: sqlException was unhandled
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("user id=student_abdo;password=XXXXXX;server=178.239.167.XXX;Trusted_Connection=yes;database=student_sms;connection timeout=30");
SqlCommand com = new SqlCommand("UPDATE `sms` SET `id`=23 WHERE `sms`='hi'",con);
com.CommandType = CommandType.Text;
con.Open();
com.ExecuteNonQuery();
con.Close();
}
The SqlClient namespace is for connecting to Microsoft SqlServer databases. If you want to work with MySql, you'll need to find an ADO.NET implementation (3rd party) or determine if there is a way to make it work with OleDb/Odbc.
UPDATE
Apparently, MySql provides its own ADO.NET driver for getting the job done.
As Brian pointed out, you are using the wrong provider. Also, your connection string is wrong. It should read
"Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
You can reference this site for all of the different connection string types for mysql
Here is what you need to download
Keep in mind that Classes for working with MySql in .NET are mostley the same as working with MSSQL with exception that they have My... prefix
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("userid=student_abdo;password=XXXXXX;server=178.239.167.XXX;Trusted_Connection=yes;database=student_sms;connection timeout=30");
MySqlCommand com = new MySqlCommand("UPDATE `sms` SET `id`=23 WHERE `sms`='hi'",con);
com.CommandType = CommandType.Text;
con.Open();
com.ExecuteNonQuery();
con.Close();
}
first of all, you know what is forms of connect string and then established the connection with the database.
bb rec
private void btnretrive_Click(object sender, EventArgs e)
{
string cs = "server=localhost;user id=root;database=world;";
SqlConnection conn = new SqlConnection(cs);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from world;",conn);
SqlDataReader reader = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(reader);
dataGridView1.DataSource = table;
conn.Close();
}