Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,611 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 914 people online right now. Registration is fast and FREE... Join Now!




Read ComboBox items from text and updating in MySQL database.

 
Reply to this topicStart new topic

Read ComboBox items from text and updating in MySQL database., Items from text file, want to select the item with the database...

MaxMow
post 2 Jul, 2008 - 07:12 AM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 2

Hi! First post here, I got here because of this question, thanks for your help:

I want to make a Game Master program for my game, whose data is in MySQL database on my computer(I got this part fine)
In my game, there are maps for the players, and each map has a code, like, Map A : 0, Map B :100 Map C: 560000 etc..
There is no specific order in the code values of the map..

So, in my program I want the user to find his character, and get details on his character and change them. One of those details are the map he is in.
I have a nice text file for the maps whose format is this "MapIDcode/tMapName",
Like "000000120 The House"
I have done the following to read and parse this file:
csharp

string sLine;
StreamReader sr = new StreamReader("maps.txt");
while (sr.Peek() >= 0)
{
sLine = sr.ReadLine();
string[] content = sLine.Split(new char[] { '\t' });
this.comboBox1.Items.Add(content[1]);
}
sr.Close();

Now, I made the maps show in a ComboBox, and it loads them rightly and show me the name of the maps, but I don't know how to use it with my bindingsource..
the maps stored in the database as the map id code.. so how can I do so the right ComboBox item get selected by the map id I get from the query?

for ex, Player A is on map "The House" which is Map ID 120.
I want that when the user types "A" in the Search box the item "The House" get selected, and if he change the map, it changes the Map ID on the database.

I know how to read and edit data from the database, I made Money and others changeable and it's working, but I got stuck on the maps.

Please help?

Thanks,
Gal.

This post has been edited by MaxMow: 2 Jul, 2008 - 11:10 AM
User is offlineProfile CardPM

Go to the top of the page

MaxMow
post 2 Jul, 2008 - 12:22 PM
Post #2


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 2

OK, I got it. I'll show you here how I solved it:

I made a new class named Map:
csharp
class Map
{
private string[] MapNames;
private int[] MapIDs;

public Map()
{
string sLine;
StreamReader sr = new StreamReader("maps.txt");
string[] MapNames = new string[1221];
int[] MapIDs = new int[1221];
for (int i = 0; i < 1221; i++)
{
sLine = sr.ReadLine();
string[] content = sLine.Split(new char[] { '\t' });
MapIDs[i] = int.Parse(content[0]);
MapNames[i] = content[1];
}
sr.Close();
this.MapIDs = MapIDs;
this.MapNames = MapNames;
}

public string GetMapNameByIndex(int index)
{
return this.MapNames[index];
}

public int GetMapIDByIndex(int index)
{
return this.MapIDs[index];
}

public int GetMapIDByName(string name)
{
for (int m = 0; m < 1221; m++)
{
if (this.MapNames[m].Equals(name))
return MapIDs[m];
}
return 0;
}

public string GetMapNameById(int id)
{
for (int m = 0; m < 1221; m++)
{
if (this.MapIDs[m].Equals(id))
return MapNames[m];
}
return "";
}

public int GetIndexByID(string id)
{
for (int m = 0; m < 1221; m++)
{
if (this.MapIDs[m].ToString().Equals(id))
return m;
}
return 0;
}
}

Now, I created a new SQL Query manually, Which returns the Map ID of a charcater, which takes a character ID parameter.
Now, in my search button clicked event I put this code:
csharp

private void CharSBTN1_Click(object sender, EventArgs e)
{
try
{
charactersTableAdapter1.FillBy(gameDataSet1.characters, SearchBox.Text);
this.IDBox.Visible = true;
this.NameBox.Visible = true;
this.MoneyBox.Visible = true;
this.comboBox1.Visible = true;
this.button1.Visible = true;
string t = charactersTableAdapter1.GetMapbyID(IDBox.Text).ToString();
this.comboBox1.SelectedIndex = Map.GetIndexByID(t);
this.Size = new System.Drawing.Size(351, 193);
}
catch (SystemException ex)
{
this.comboBox1.SelectedItem = null;
MessageBox.Show("Not Exist!");
}
}

There, Now it shows the map the chacter is on. But how can I change it? I got stuk here for a couple of minutes, thinking about binding but then I thought about making another query!
Now, here's the code on my save button click event:
csharp

private void button1_Click(object sender, EventArgs e)
{
try
{
int mapi = Map.GetMapIDByIndex(comboBox1.SelectedIndex);
charactersTableAdapter1.UpdateLim(NameBox.Text, mapi, int.Parse(MesoBox.Text), int.Parse(IDBox.Text));
MessageBox.Show("SAVED!", "Char edit mode");
}
catch (SystemException ex)
{
MessageBox.Show("Fuck you Idiot!"); //Temp LOL
}
}

There! That's how I solved it!


Those are my query if you want to see it:

The Table Adapter filling query:
sql
SELECT     ID, name, userid, world_id, level, job, str, dex, `int`, luk, chp, mhp, cmp, mmp, hpmp_ap, ap, sp, exp, fame, map, pos, gender, skin, eyes, hair, mesos, 
buddylist_size, online
FROM characters
WHERE (name = @name)

The map getting query:
sql
SELECT     map
FROM characters
WHERE (ID = @ID)

And the updating query, used in the save button:
sql
UPDATE    characters
SET name = @name, map = @map, mesos = @mesos
WHERE (ID = @Original_ID)


Well, I hope my solution is good, but if you got anything to say, say it!


This post has been edited by MaxMow: 3 Jul, 2008 - 12:23 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:32AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month