Complex model binding in ado.net












1















I am getting a table from database. The table is getting using joins of other table. I am struggling to bind it with my model. Insert data into both table is working.



I am getting the values from database but don't know how to convert address into list.



public class address
{
public int id { get; set; }
public string peopleaddress { get; set; }
}

public class People
{
public int id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }

public string address { get; set; }
public List<string> cityid { get; set; } // is equal to people address
public string shortimagepath { get; set; }
public string fullimagepath { get; set; }
}


This one is my dbcontext class using ado.net



    public List<People> selectallpeople()
{
List<People> peopleslist = new List<People>();
List<address> addresses = new List<address>();

using (SqlConnection sqlConnection=new SqlConnection(dbconnect))
{
SqlCommand sqlCommand = new SqlCommand("selectallpeople", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable datatable = new DataTable();
sqlDataAdapter.Fill(datatable);

foreach (DataRow item in datatable.Rows)
{
addresses.Add(new address
{
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
});
}

return peopleslist;
}


I am getting all values from database but I need to bind multiple address to list of addresses.










share|improve this question

























  • Maybe you can clarify your question more...

    – NoChance
    Jan 1 at 8:14











  • @NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

    – ahmedshah
    Jan 1 at 9:08











  • I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

    – Jitendra G2
    Jan 1 at 9:47
















1















I am getting a table from database. The table is getting using joins of other table. I am struggling to bind it with my model. Insert data into both table is working.



I am getting the values from database but don't know how to convert address into list.



public class address
{
public int id { get; set; }
public string peopleaddress { get; set; }
}

public class People
{
public int id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }

public string address { get; set; }
public List<string> cityid { get; set; } // is equal to people address
public string shortimagepath { get; set; }
public string fullimagepath { get; set; }
}


This one is my dbcontext class using ado.net



    public List<People> selectallpeople()
{
List<People> peopleslist = new List<People>();
List<address> addresses = new List<address>();

using (SqlConnection sqlConnection=new SqlConnection(dbconnect))
{
SqlCommand sqlCommand = new SqlCommand("selectallpeople", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable datatable = new DataTable();
sqlDataAdapter.Fill(datatable);

foreach (DataRow item in datatable.Rows)
{
addresses.Add(new address
{
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
});
}

return peopleslist;
}


I am getting all values from database but I need to bind multiple address to list of addresses.










share|improve this question

























  • Maybe you can clarify your question more...

    – NoChance
    Jan 1 at 8:14











  • @NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

    – ahmedshah
    Jan 1 at 9:08











  • I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

    – Jitendra G2
    Jan 1 at 9:47














1












1








1








I am getting a table from database. The table is getting using joins of other table. I am struggling to bind it with my model. Insert data into both table is working.



I am getting the values from database but don't know how to convert address into list.



public class address
{
public int id { get; set; }
public string peopleaddress { get; set; }
}

public class People
{
public int id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }

public string address { get; set; }
public List<string> cityid { get; set; } // is equal to people address
public string shortimagepath { get; set; }
public string fullimagepath { get; set; }
}


This one is my dbcontext class using ado.net



    public List<People> selectallpeople()
{
List<People> peopleslist = new List<People>();
List<address> addresses = new List<address>();

using (SqlConnection sqlConnection=new SqlConnection(dbconnect))
{
SqlCommand sqlCommand = new SqlCommand("selectallpeople", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable datatable = new DataTable();
sqlDataAdapter.Fill(datatable);

foreach (DataRow item in datatable.Rows)
{
addresses.Add(new address
{
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
});
}

return peopleslist;
}


I am getting all values from database but I need to bind multiple address to list of addresses.










share|improve this question
















I am getting a table from database. The table is getting using joins of other table. I am struggling to bind it with my model. Insert data into both table is working.



I am getting the values from database but don't know how to convert address into list.



public class address
{
public int id { get; set; }
public string peopleaddress { get; set; }
}

public class People
{
public int id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }

public string address { get; set; }
public List<string> cityid { get; set; } // is equal to people address
public string shortimagepath { get; set; }
public string fullimagepath { get; set; }
}


This one is my dbcontext class using ado.net



    public List<People> selectallpeople()
{
List<People> peopleslist = new List<People>();
List<address> addresses = new List<address>();

using (SqlConnection sqlConnection=new SqlConnection(dbconnect))
{
SqlCommand sqlCommand = new SqlCommand("selectallpeople", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable datatable = new DataTable();
sqlDataAdapter.Fill(datatable);

foreach (DataRow item in datatable.Rows)
{
addresses.Add(new address
{
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
});
}

return peopleslist;
}


I am getting all values from database but I need to bind multiple address to list of addresses.







c# sql asp.net sql-server asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 18:39









JohnyL

3,7231924




3,7231924










asked Jan 1 at 7:39









ahmedshahahmedshah

85




85













  • Maybe you can clarify your question more...

    – NoChance
    Jan 1 at 8:14











  • @NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

    – ahmedshah
    Jan 1 at 9:08











  • I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

    – Jitendra G2
    Jan 1 at 9:47



















  • Maybe you can clarify your question more...

    – NoChance
    Jan 1 at 8:14











  • @NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

    – ahmedshah
    Jan 1 at 9:08











  • I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

    – Jitendra G2
    Jan 1 at 9:47

















Maybe you can clarify your question more...

– NoChance
Jan 1 at 8:14





Maybe you can clarify your question more...

– NoChance
Jan 1 at 8:14













@NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

– ahmedshah
Jan 1 at 9:08





@NoChance how to bind a coloumn with public List<string> cityid .a person has many addresses so how to bind those many addresses to a person simple

– ahmedshah
Jan 1 at 9:08













I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

– Jitendra G2
Jan 1 at 9:47





I am not clear what you want! but may be helpfull string addressListSepartedbyCommas = string.Join(",", address Array).

– Jitendra G2
Jan 1 at 9:47












1 Answer
1






active

oldest

votes


















0














You can refer my solution. Hope to help and Happy new year, my friend :))



foreach (DataRow item in datatable.Rows)
{
int idObj = (int)item["id"] //This Id for both, right?
addresses.Add(new address
{
id = idObj,
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
cityid = addresses.Where(t => t.id == idObj)
.Select(t=>t.peopleaddress.ToString()).ToList()
});


}


In the View you can show cityid like this:



foreach(var item in peopleslist)
{
var lstCityId = String.Join(",", item.cityid).ToString();
}





share|improve this answer


























  • its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

    – ahmedshah
    Jan 1 at 18:01













  • Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

    – JohnyL
    Jan 1 at 18:37











  • @ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

    – Tomato32
    Jan 2 at 12:49













  • @JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

    – Tomato32
    Jan 2 at 12:50











  • Now that's good! Happy New Year! 😉

    – JohnyL
    Jan 2 at 17:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993826%2fcomplex-model-binding-in-ado-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can refer my solution. Hope to help and Happy new year, my friend :))



foreach (DataRow item in datatable.Rows)
{
int idObj = (int)item["id"] //This Id for both, right?
addresses.Add(new address
{
id = idObj,
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
cityid = addresses.Where(t => t.id == idObj)
.Select(t=>t.peopleaddress.ToString()).ToList()
});


}


In the View you can show cityid like this:



foreach(var item in peopleslist)
{
var lstCityId = String.Join(",", item.cityid).ToString();
}





share|improve this answer


























  • its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

    – ahmedshah
    Jan 1 at 18:01













  • Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

    – JohnyL
    Jan 1 at 18:37











  • @ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

    – Tomato32
    Jan 2 at 12:49













  • @JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

    – Tomato32
    Jan 2 at 12:50











  • Now that's good! Happy New Year! 😉

    – JohnyL
    Jan 2 at 17:11
















0














You can refer my solution. Hope to help and Happy new year, my friend :))



foreach (DataRow item in datatable.Rows)
{
int idObj = (int)item["id"] //This Id for both, right?
addresses.Add(new address
{
id = idObj,
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
cityid = addresses.Where(t => t.id == idObj)
.Select(t=>t.peopleaddress.ToString()).ToList()
});


}


In the View you can show cityid like this:



foreach(var item in peopleslist)
{
var lstCityId = String.Join(",", item.cityid).ToString();
}





share|improve this answer


























  • its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

    – ahmedshah
    Jan 1 at 18:01













  • Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

    – JohnyL
    Jan 1 at 18:37











  • @ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

    – Tomato32
    Jan 2 at 12:49













  • @JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

    – Tomato32
    Jan 2 at 12:50











  • Now that's good! Happy New Year! 😉

    – JohnyL
    Jan 2 at 17:11














0












0








0







You can refer my solution. Hope to help and Happy new year, my friend :))



foreach (DataRow item in datatable.Rows)
{
int idObj = (int)item["id"] //This Id for both, right?
addresses.Add(new address
{
id = idObj,
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
cityid = addresses.Where(t => t.id == idObj)
.Select(t=>t.peopleaddress.ToString()).ToList()
});


}


In the View you can show cityid like this:



foreach(var item in peopleslist)
{
var lstCityId = String.Join(",", item.cityid).ToString();
}





share|improve this answer















You can refer my solution. Hope to help and Happy new year, my friend :))



foreach (DataRow item in datatable.Rows)
{
int idObj = (int)item["id"] //This Id for both, right?
addresses.Add(new address
{
id = idObj,
peopleaddress = item["address"].ToString(),
});

peopleslist.Add(new People
{
id = (int)item["id"],
Name = item["name"].ToString(),
LastName = item["lastname"].ToString(),
shortimagepath = item["imageshortpath"].ToString(),
// I am struggling here to bind address column to list of address
cityid = addresses.Where(t => t.id == idObj)
.Select(t=>t.peopleaddress.ToString()).ToList()
});


}


In the View you can show cityid like this:



foreach(var item in peopleslist)
{
var lstCityId = String.Join(",", item.cityid).ToString();
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 13:07

























answered Jan 1 at 9:51









Tomato32Tomato32

1,091157




1,091157













  • its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

    – ahmedshah
    Jan 1 at 18:01













  • Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

    – JohnyL
    Jan 1 at 18:37











  • @ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

    – Tomato32
    Jan 2 at 12:49













  • @JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

    – Tomato32
    Jan 2 at 12:50











  • Now that's good! Happy New Year! 😉

    – JohnyL
    Jan 2 at 17:11



















  • its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

    – ahmedshah
    Jan 1 at 18:01













  • Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

    – JohnyL
    Jan 1 at 18:37











  • @ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

    – Tomato32
    Jan 2 at 12:49













  • @JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

    – Tomato32
    Jan 2 at 12:50











  • Now that's good! Happy New Year! 😉

    – JohnyL
    Jan 2 at 17:11

















its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

– ahmedshah
Jan 1 at 18:01







its seems like binding all address to every customer i need to bind one customer to their many address not everyaddress.@tomato32

– ahmedshah
Jan 1 at 18:01















Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

– JohnyL
Jan 1 at 18:37





Please, improve readability of your answer. When you write the answer, there's a preview beneath it. Consider taking a look at it.

– JohnyL
Jan 1 at 18:37













@ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

– Tomato32
Jan 2 at 12:49







@ahmedshah: You need to filter the data first. I edited my solution. Please refer it. Sorry for responding late my friend.

– Tomato32
Jan 2 at 12:49















@JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

– Tomato32
Jan 2 at 12:50





@JohnyL: I'm really sorry about that. I edited it. Happy new year, my friend :))

– Tomato32
Jan 2 at 12:50













Now that's good! Happy New Year! 😉

– JohnyL
Jan 2 at 17:11





Now that's good! Happy New Year! 😉

– JohnyL
Jan 2 at 17:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993826%2fcomplex-model-binding-in-ado-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas