Sql anywhere 12 connection string - sqlanywhere

I am trying to connect sql anywhere12 database to my .net project but while connecting to the database i am facing an error
"{"Invalid connection string. Error parsing connection parameter string\r\nParameter name: connectionString"}"
while running this code.
private void btncon_Click(object sender, EventArgs e)
{
string connetionString = null;
SAConnection connection;
SACommand command;
string sqla = null;
// connetionString = "Data Source=SQL Anywhere 12 Demo;Initial Catalog=Demo2;User ID=dba;Password=sql";
connetionString = "Driver={SQL Anywhere 12};ENG=demo12;UID=dba;PWD=sql;DBN=sql;LINKS=TCPIP(HOST=localhost)";
sqla = "INSERT INTO student(Rollnumber,Name) values('" + textBox1.Text + "','" + textBox2.Text + "')";
connection = new SAConnection(connetionString);
try
{
connection.Open();
command = new SACommand(sqla, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
actually i have no much knowledge about the sql anywhere 12 connection string please help me to solve this error

Related

org.postgresql.util.PSQLException: This ResultSet is closed

When I am trying to print the row values from a table in a PostgreSQL database using JDBC with this code
static Connection c = null;
static Statement statement = null;
static Scanner sc = new Scanner(System.in);
public static void displayTable() throws Exception{
ResultSet rs = statement.executeQuery("SELECT * FROM TABLE");
String dataBaseName = "";
String dataBaseNumber = "";
try {
while(rs.next()){
//System.out.println("Resultant set: " + rs);
dataBaseName = rs.getString("name");
dataBaseNumber = rs.getString("number");
System.out.println("Name: " + dataBaseName + "\Number: " + dataBaseNumber);
}
} catch (Exception e) {
System.out.println(e);
}
}
In while loop, after printing first row it gives me exception
org.postgresql.util.PSQLException: This ResultSet is closed.
Can some one please explain what's going on?

Connections going into close_wait

Since so many days we are facing the problem of close_wait connection..My doubt was that not all the connections with that particular server are going into close wait.If it would have been problem with the code then this problem should be with every connection I am making with the server.Destination server is apache server and source server is glassfish . I have also added header for closing connection according to this article.http://www.michaelprivat.com/?p=63 But no luck I am posting code which I have used for making connection with the target server .Is there any problem while making connection?
public String Consumer(String userId, String Type) throws IOException {
CloseableHttpClient hc = HttpClients.createDefault();
try {
String response = null;
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://IP and port of th target server/PolicyService.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", userId));
nameValuePairs.add(new BasicNameValuePair("Type", Type));
nameValuePairs.add(new BasicNameValuePair("token", Token));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
postMethod.addHeader("Connection", "close");
response = hc.execute(postMethod, res);
return response;
} catch (HttpResponseException g) {
logger.error("Exception occurred of type: " , g);
return "";
} catch (UnsupportedEncodingException e) {
logger.error("Exception occurred of type: " , e);
return "";
} catch (ClientProtocolException e) {
logger.error("Exception occurred of type: " , e);
return "";
} catch (IOException e) {
logger.error("Exception occurred of type: " , e);
return "";
}
finally{
hc.close();
}
}
everytime I have to restart server to flush this connections .Any sugestions?

In what order C# execute sqlstatement?

I have two table related by foreign and primary key constraint.The Visit_Number in Patient table must exist in the Visit table.In my code in define create two instance of my connection string such that i can use one instance to insert record:VisitNumber in Visit table first and then the other instance to insert record in the patient table,with the believe that the cord runs from top to bottom.But this was n't the case. i still get foreign key constraint errror:
Error Number:547
Error MessageThe INSERT statement conflicted with the FOREIGN KEY constraint "Patient_Vist_FK".
The conflict occurred in database "TestDB", table "dbo.Visit", column 'Visit_Number'.
The statement has been terminated.On line Number: 1
meaning the code is running as i expected. Please do you have a better approach and why isn't mine working
code:
protected void btn_save_Click(object sender, EventArgs e)
{
string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
SqlConnection con = new SqlConnection(connect);
SqlConnection con2 = new SqlConnection(connect);
string visitnumber = txtVistNumber.Text.ToString();
string insert_statement = "Insert into Patient(Patient_Number,FirstName,LastName,Gender,Tribe,Date_Of_Birth,Visit_Number)"
+ "Values(#Patient_Number,#FirstName,#LastName,#Gender,#Tribe,#Date_Of_Birth,#Visit_Number)";
string insert_stament2 = "Insert into Visit(Visit_Number)"
+ "Values(#Visit_Number)";
SqlCommand cmd = new SqlCommand(insert_statement, con);
SqlCommand cmd2 = new SqlCommand(insert_stament2, con2);
cmd2.Parameters.AddWithValue("#Visit_Number", txtVistNumber.Text);
cmd.Parameters.AddWithValue("#Patient_Number",txtpatientNum.Text);
cmd.Parameters.AddWithValue("#FirstName",txtfirstName.Text);
cmd.Parameters.AddWithValue("#LastName",txtlastname.Text);
cmd.Parameters.AddWithValue("#Gender", drl_gender.SelectedValue);
cmd.Parameters.AddWithValue("#Tribe",DropDownList1.Text);
cmd.Parameters.AddWithValue("#Date_Of_Birth", val_age.Text);
cmd.Parameters.AddWithValue("#Visit_Number", txtVistNumber.Text);
try
{
using (con)
{
con.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
Response.Write("<script language=javascript>alert('Record Sucessfully Inserted!');</script>");
//Success_Message.Text = "Record inserted";
txtpatientNum.Text = String.Empty;
txtfirstName.Text = String.Empty;
txtlastname.Text = String.Empty;
txtVistNumber.Text = String.Empty;
DropDownList1.Text = String.Empty;
val_age.Text = String.Empty;
}
}
}
catch (SqlException ex)
{
{
VisitError_Message.Text = "Error Number:" + ex.Number.ToString() + " Error Message" + ex.Message + "On line Number" + ": " + ex.LineNumber;
}
}
catch (NullReferenceException nullexception)
{
VisitError_Message.Text = "Error Occurred, Error Type:" + nullexception.GetType().ToString() + "Error Message:" + nullexception.Message;
}
catch (DllNotFoundException dllexception)
{
VisitError_Message.Text = dllexception.GetType().ToString() + dllexception.Message;
}
finally
{
con.Close();
}
}
you not excuting you cmd2, you must execute the insert Visit_Number in cmd2 then excute your cmd, you can test this code
using (con2)
{
con2.Open();
cmd2.ExecuteNonQuery();
}
then you can excute your cmd
using (con)
{
con.Open();
int count = cmd.ExecuteNonQuery();
}
and you can do the work with the same connexion if you want
string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
SqlConnection con = new SqlConnection(connect);
string visitnumber = txtVistNumber.Text.ToString();
string insert_statement = "Insert into Patient(Patient_Number,FirstName,LastName,Gender,Tribe,Date_Of_Birth,Visit_Number)"
+ "Values(#Patient_Number,#FirstName,#LastName,#Gender,#Tribe,#Date_Of_Birth,#Visit_Number)";
string insert_stament2 = "Insert into Visit(Visit_Number)"
+ "Values(#Visit_Number)";
using(con)
{
con.open;
SqlCommand cmd2 = new SqlCommand(insert_stament2, con);
cmd2.Parameters.AddWithValue("#Visit_Number", txtVistNumber.Text);
cmd2.ExecuteNonQuery();
SqlCommand cmd = new SqlCommand(insert_statement, con);
cmd.Parameters.AddWithValue("#Visit_Number", txtVistNumber.Text);
cmd.Parameters.AddWithValue("#Patient_Number",txtpatientNum.Text);
cmd.Parameters.AddWithValue("#FirstName",txtfirstName.Text);
cmd.Parameters.AddWithValue("#LastName",txtlastname.Text);
cmd.Parameters.AddWithValue("#Gender", drl_gender.SelectedValue);
cmd.Parameters.AddWithValue("#Tribe",DropDownList1.Text);
cmd.Parameters.AddWithValue("#Date_Of_Birth", val_age.Text);
cmd.Parameters.AddWithValue("#Visit_Number", txtVistNumber.Text);
cmd.ExecuteNonQuery();
}

How update DB table with DataSet

I am begginer with ADO.NET , I try update table with DataSet.
O client side I have dataset with one table. I send this dataset on service side (it is ASP.NET Web Service).
On Service side I try update table in database, but it dont 't work.
public bool Update(DataSet ds)
{
SqlConnection conn = null;
SqlDataAdapter da = null;
SqlCommand cmd = null;
try
{
string sql = "UPDATE * FROM Tab1";
string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString;
conn = new SqlConnection(connStr);
conn.Open();
cmd=new SqlCommand(sql,conn);
da = new SqlDataAdapter(sql, conn);
da.UpdateCommand = cmd;
da.Update(ds.Tables[0]);
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn != null)
conn.Close();
if (da != null)
da.Dispose();
}
}
Where can be problem?
It is better to look how really ADO.Net dataset works.
http://support.microsoft.com/kb/308507

Error in ExecuteReader

I am getting the following error message:
System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed.
And here is my code:
public IDataReader ExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
Does anyone know how I can resolve this?
The Connection object that your SQLCommand is attached to has not been opened. You must open the connection before you can query.
Something like this:
private static void OpenSqlConnection(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
// Do your command access here.
}
}