program not updating database [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Im trying to create a form that allows users to edit a value in the database. My code below runs without error however
This is the calling method
private void BTNSavePool_Click(object sender, EventArgs e)
{
RWCStatTracker.Database.CLSDB.EditPool(TXTEditPool.Text, CMBBXSearchPool.SelectedItem.ToString());
CMBBXSearchPool.Text = "Please select a Pool....";
MessageBox.Show("Pool edited", "Alert");
this.FRMEditPool_Load(this, null);
PNLEditPoolSearch.Show();
}
This is the code in my database connection class
public static void EditPool(String OldName, String NewName)
{
string UPDTStmt = "UPDATE TBL_Pool SET Name = #NewName WHERE Name = #OldName";
SqlConnection conn = GetConnection();
SqlCommand UPDTCmd = new SqlCommand(UPDTStmt, conn);
UPDTCmd.Parameters.AddWithValue("#NewName", NewName);
UPDTCmd.Parameters.AddWithValue("#OldName", OldName);
try { conn.Open(); UPDTCmd.ExecuteNonQuery(); }
catch (SqlException ex) { throw ex; }
finally { conn.Close(); }
}
Any ideas why it's not updating?

Most likely the WHERE clause isn't selecting any records.
It may have spaces: "Smith " will not match "Smith" or it may be case sensitive: "Smith" will not match "smith".
Check the data to see if it has spaces and string trim your parameters.

I've just figured out the issue with it, the arguments are in the wrong order
This
public static void EditPool(String OldName, String NewName)
Should be
public static void EditPool(String NewName, String OldName)

Related

Check if cyclic string or not

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 hours ago.
Improve this question
Enter a string of characters S. Indicates whether the string S is cyclic or not.
For example S='abcdabcdabcdabcd' is cyclic.
My teacher want me to do this but i think it's too hard. I'm thinking using SplitString but it's just an idea and i dont know how to do. I'm very thankful if someone gives me help
using System;
using System.Collections;
public class MYCLASSNAME {
public static void Main(string[] args){
var str = "abcdabcdabcdabcd";
Console.Write(checkCyclicString(str));
}
static bool checkCyclicString (string str){
var checkString ="";
for(var i=0;i<str.Length-1;i++){
checkString= checkString+ str[i];
var countOfCycles =0;
var haveCycles = false;
for(var j=i+1;j<str.Length-checkString.Length+1;j= j+checkString.Length){
if(checkString==str.Substring(j,checkString.Length)){
haveCycles = true;
countOfCycles++;
}else{
haveCycles =false;
}
}
if(haveCycles && countOfCycles == Math.Ceiling((double)(str.Length/checkString.Length))-1){
return true;
}
}
return false;
}
}
check the cyclic behavior by creating substrigs and comparing them with all the parts of the given string

Method wont work C# When still called out on main [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a new coder and I don't fully understand coding C# but my method wont work for some reason and I did call it out on main.
static void Main(string[] args)
{
BOT();
}
void BOT()
{
Random numberGen = new Random();
String ID = "A.P.B" + numberGen.Next(50, 1000);
int serial = numberGen.Next(10000000, 1000000000);
String PU = "AMD RYZEN" + numberGen.Next(1, 11);
String model = "PAC" + numberGen.Next(10, 1000);
Console.WriteLine("Hello, My name or my Combat ID is" + ID);
}
Your call to BOT is in a static method. You either need BOT to be static or you need to create an instance of the class that contains BOT to be able to call it.
static void BOT()
{
Random numberGen = new Random();
String ID = "A.P.B" + numberGen.Next(50, 1000);
int serial = numberGen.Next(10000000, 1000000000);
String PU = "AMD RYZEN" + numberGen.Next(1, 11);
String model = "PAC" + numberGen.Next(10, 1000);
Console.WriteLine("Hello, My name or my Combat ID is" + ID);
}
When I run it now I get:
Hello, My name or my Combat ID isA.P.B188

Database creation only on first run of visual c# application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Any one help me to create a database in MySQL only on the first run of my c# application. Second time when i run this application if database exists then creation of database code should be skipped.
If you use code first in the Entity Framework you can create your database from the classes in C# that define you tables and relationships.
Once you have your classes you just have to change or setup a connection string to your database and then run code... then the database will be created for you... by magic .. or your classes!!
There are many samples online
Code First with Azure
http://www.dotnetjalps.com/2015/04/entity-framework-code-first--mysql-azure.html
Entity Framework Code First - Create Database with MySql?
private static bool CheckDatabaseExists(SqlConnection tmpConn, string databaseName)
{
string sqlCreateDBQuery;
bool result = false;
try
{
tmpConn = new SqlConnection("server=(local)\\SQLEXPRESS;Trusted_Connection=yes");
sqlCreateDBQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name
= '{0}'", databaseName);
using (tmpConn)
{
using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
{
tmpConn.Open();
object resultObj = ExecuteScalar();
int databaseID = 0;
if (resultObj != null)
{
int.TryParse(resultObj.ToString(), out databaseID);
}
tmpConn.Close();
result = (databaseID > 0);
}
}
}
catch (Exception ex)
{
result = false;
}
return result;
}
This will work with any database name you pass in as a parameter, and it will return a bool true = database exists, false = database does not exist (or error happened).If true mean skip the creation false mean create database..

How do I "Unit Test" in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I have this method in my project:
public static String MD5Hash(string TextToHash)
{
if ((TextToHash == null) || (TextToHash.Length == 0))
{
return String.Empty;
}
MD5 md5 = new MD5CryptoServiceProvider();
byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
byte[] result = md5.ComputeHash(textToHash);
return System.BitConverter.ToString(result);
}
And I've tried testing like this:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BugMon;
namespace BugMonTesting
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string pwd = "Password";
string expected = "DC-64-7E-B6-5E-67-11-E1-55-37-52-18-21-2B-39-64";
frmLogIn.MD5Hash(pwd);
Assert.AreEqual(pwd, expected);
}
}
}
But the string pwd does not seem to be passing through the Method when I run the test and stays as "Password".
What am I doing wrong?
Sorry if this is obvious but I've never had to use these tests before.
You're never doing anything with the return value from MD5Hash.
Try this:
string hash = frmLogIn.MD5Hash(pwd);
Assert.AreEqual(hash, expected);
Note that this will only work if MD5Hash returns a string formatted like the expected variable.

XML File reading string failed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to read from XML file.
I success to read an int. but when I want to convert it so string it doesn't works. I would like to get some help.
XML:
<Data>
<ServerClient>1</ServerClient>
<ClientIP>127.0.0.1</ClientIP>
<ClientPort>11000</ClientPort>
</Data>
The function getType reads good the int in the XML file.
private XmlDocument doc;
public int getType()
{
try
{
// Open the file again
doc.Load("ServerClientXML.xml");
// Read port
XmlNode node = doc.SelectSingleNode("/Data/ServerClient");
return int.Parse(node.InnerText); // 0 = Server, 1 = Client
}
catch
{
return -1;
}
}
public string getIP()
{
string ip;
XmlNode node;
try
{
// Open the file again
doc.Load("ServerClientXML.xml");
int Type = getType();
if (Type == 1) // Client type
{
// Read IP
node = doc.SelectSingleNode("/Data/ServerClient/ClientIP");
ip = doc.InnerXml;
}
else // Server Type
{
// Read IP
node = doc.SelectSingleNode("/Data/ServerClient/ServerIP");
ip = doc.InnerXml;
}
return ip;
}
catch
{
return null;
}
}
I have tried to to like getType but without any success :
return node.InnerText.toString(); // 0 = Server, 1 = Client
Your XPath doesn't match provided XML. It should be /Data/ClientIP and /Data/ServerIP

Categories

Resources